返回 const 引用时出现问题(没有对 const 的引用)
Problems returning a const reference (no a reference to a const)
我想 return 一个 const 引用,这样我就不能更改对象本身,但我可以调用它们的非常量方法。但是我不知道怎么做。
使用指针很容易,我可以在常量指针 (const myclass*) 或指向常量值的指针 (myclass const*) 之间进行选择。但是对于引用它似乎不是以同样的方式工作,我所得到的只是对 const 对象的引用所以我不能调用非 const 方法。
我确定我做错了什么。
class A {
int a;
public:
auto get() const { return a; }
auto set(int i) -> void { a = i; }
};
class B {
A a_;
public:
auto get() const -> const A& { return a_; }
};
我能做到:
B b;
cout << b.get().get();
但不是:
B b;
b.get().set(100); // compiler error
我不要
class B {
A a_;
public:
auto get() -> A& { return a_; }
};
B b;
A a;
b.get() = a; // I don't want this!
Problems returning a const reference (no a reference to a const)
技术上不存在 const 引用这样的东西。参考文献没有顶级 cv-qualifiers。 const reference 通俗地说表示对const.
的引用
I want to return a ... reference so I cannot change the object itself
不能通过对 const 的引用来修改对象,因此可以。
... but I [want to] call their non-const methods.
Non-const 成员函数只能通过引用 non-const 来调用。没有哪一类参考资料可以同时满足您的需求。他们是矛盾的。
With pointers is easy, I can choose between a const pointer (const myclass*
) or a pointer to const values (myclass const*
).
那些也达不到你想要的。可以通过const指针(指向non-const)修改指向对象,不能通过指向const的(non-const)指针调用指向对象的const成员函数。两者都达不到你的要求。
P.S。返回一个 const(或 volatile)限定指针 - 或任何其他 built-in 类型 - 是没有意义的,因为对此类函数的函数调用是纯右值表达式,而 non-class 类型的纯右值没有 cv-qualifiers,所以这种 return 类型的限定符将始终被忽略。这不要与 returning 指向有意义的 cv-qualified 类型的指针混淆。
你不能以这种方式拥有“部分可变性”,而不是从 A
.
中完全删除赋值
(尽管您声称使用指针很容易,但情况完全相同 - 您需要禁止 b.get()->set(100)
或允许 *b.get() = a
。)
您可以做的一件事是通过代理对象添加一个间接级别:
class A_Setter
{
public:
A_Setter(A* a): the_a(a) {}
A_Setter& operator=(const A_Setter&) = delete;
void set(int x) { the_a->set(x); }
private:
A* the_a;
};
class B {
A a_;
public:
A_setter get() { return A_setter(&a_); }
};
现在 b.get().set(100);
会编译,而不是 b.get() = a;
。
我想 return 一个 const 引用,这样我就不能更改对象本身,但我可以调用它们的非常量方法。但是我不知道怎么做。
使用指针很容易,我可以在常量指针 (const myclass*) 或指向常量值的指针 (myclass const*) 之间进行选择。但是对于引用它似乎不是以同样的方式工作,我所得到的只是对 const 对象的引用所以我不能调用非 const 方法。
我确定我做错了什么。
class A {
int a;
public:
auto get() const { return a; }
auto set(int i) -> void { a = i; }
};
class B {
A a_;
public:
auto get() const -> const A& { return a_; }
};
我能做到:
B b;
cout << b.get().get();
但不是:
B b;
b.get().set(100); // compiler error
我不要
class B {
A a_;
public:
auto get() -> A& { return a_; }
};
B b;
A a;
b.get() = a; // I don't want this!
Problems returning a const reference (no a reference to a const)
技术上不存在 const 引用这样的东西。参考文献没有顶级 cv-qualifiers。 const reference 通俗地说表示对const.
的引用I want to return a ... reference so I cannot change the object itself
不能通过对 const 的引用来修改对象,因此可以。
... but I [want to] call their non-const methods.
Non-const 成员函数只能通过引用 non-const 来调用。没有哪一类参考资料可以同时满足您的需求。他们是矛盾的。
With pointers is easy, I can choose between a const pointer (
const myclass*
) or a pointer to const values (myclass const*
).
那些也达不到你想要的。可以通过const指针(指向non-const)修改指向对象,不能通过指向const的(non-const)指针调用指向对象的const成员函数。两者都达不到你的要求。
P.S。返回一个 const(或 volatile)限定指针 - 或任何其他 built-in 类型 - 是没有意义的,因为对此类函数的函数调用是纯右值表达式,而 non-class 类型的纯右值没有 cv-qualifiers,所以这种 return 类型的限定符将始终被忽略。这不要与 returning 指向有意义的 cv-qualified 类型的指针混淆。
你不能以这种方式拥有“部分可变性”,而不是从 A
.
中完全删除赋值
(尽管您声称使用指针很容易,但情况完全相同 - 您需要禁止 b.get()->set(100)
或允许 *b.get() = a
。)
您可以做的一件事是通过代理对象添加一个间接级别:
class A_Setter
{
public:
A_Setter(A* a): the_a(a) {}
A_Setter& operator=(const A_Setter&) = delete;
void set(int x) { the_a->set(x); }
private:
A* the_a;
};
class B {
A a_;
public:
A_setter get() { return A_setter(&a_); }
};
现在 b.get().set(100);
会编译,而不是 b.get() = a;
。