Meaning of 'const' last in a function declaration of a class?
在这样的声明中,EDOCX1[0]的含义是什么?江户记1〔0〕把我搞糊涂了。
1 2 3 4 5 6 | class foobar { public: operator int () const; const char* foo() const; }; |
在方法中添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <iostream> class MyClass { private: int counter; public: void Foo() { std::cout <<"Foo" << std::endl; } void Foo() const { std::cout <<"Foo const" << std::endl; } }; int main() { MyClass cc; const MyClass& ccc = cc; cc.Foo(); ccc.Foo(); } |
这将输出
1 2 | Foo Foo const |
在非常量方法中,您可以更改实例成员,在
1 2 3 4 5 6 7 8 9 10 11 | void Foo() { counter++; //this works std::cout <<"Foo" << std::endl; } void Foo() const { counter++; //this will not compile std::cout <<"Foo const" << std::endl; } |
这并非完全正确,因为可以将成员标记为
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #include <iostream> class MyClass { private: mutable int counter; public: MyClass() : counter(0) {} void Foo() { counter++; std::cout <<"Foo" << std::endl; } void Foo() const { counter++; std::cout <<"Foo const" << std::endl; } int GetInvocations() const { return counter; } }; int main(void) { MyClass cc; const MyClass& ccc = cc; cc.Foo(); ccc.Foo(); std::cout <<"The MyClass instance has been invoked" << ccc.GetInvocations() <<" times" << endl; } |
哪个会输出
1 2 3 | Foo Foo const The MyClass instance has been invoked 2 times |
const意味着该方法承诺不更改类的任何成员。即使对象本身被标记为
1 2 | const foobar fb; fb.foo(); |
是合法的。
看看C++中"const"的用法和用法是多少?更多信息。
1 2 3 4 | class foobar { ... const char* bar(); } |
方法
1 2 3 4 | void func1(const foobar& fb1, foobar& fb2) { const char* v1 = fb1.bar(); // won't compile const char* v2 = fb2.bar(); // works } |
不过,
1 | foobar& fbNonConst = const_cast<foobar&>(fb1); |
这些常量意味着如果"with const"方法更改内部数据,编译器将出错。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | class A { public: A():member_() { } int hashGetter() const { state_ = 1; return member_; } int goodGetter() const { return member_; } int getter() const { //member_ = 2; // error return member_; } int badGetter() { return member_; } private: mutable int state_; int member_; }; |
测试
1 2 3 4 5 6 7 8 9 10 11 12 | int main() { const A a1; a1.badGetter(); // doesn't work a1.goodGetter(); // works a1.hashGetter(); // works A a2; a2.badGetter(); // works a2.goodGetter(); // works a2.hashGetter(); // works } |
阅读此了解更多信息
布莱尔的回答是肯定的。
但是请注意,有一个
如果希望对象记住调用特定方法的次数,而不影响该方法的"逻辑"常量,则可能需要使用此方法(例如)。
C++常用知识中一个const成员函数的含义:必要的中间程序给出了一个清晰的解释:
The type of the this pointer in a non-const member function of a class
X is X * const. That is, it’s a constant pointer to a non-constant X
(see Const Pointers and Pointers to Const [7, 21]). Because the object
to which this refers is not const, it can be modified. The type of
this in a const member function of a class X is const X * const. That
is, it’s a constant pointer to a constant X. Because the object to
which this refers is const, it cannot be modified. That’s the
difference between const and non-const member functions.
所以在你的代码中:
1 2 3 4 5 6 | class foobar { public: operator int () const; const char* foo() const; }; |
你可以这样想:
1 2 3 4 5 6 | class foobar { public: operator int (const foobar * const this) const; const char* foo(const foobar * const this) const; }; |
当你在方法签名中使用
我想增加以下几点。
你也可以把它变成一个
所以,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | struct s{ void val1() const { // *this is const here. Hence this function cannot modify any member of *this } void val2() const & { // *this is const& here } void val3() const && { // The object calling this function should be const rvalue only. } void val4() && { // The object calling this function should be rvalue reference only. } }; int main(){ s a; a.val1(); //okay a.val2(); //okay // a.val3() not okay, a is not rvalue will be okay if called like std::move(a).val3(); // okay, move makes it a rvalue } |
请随时改进答案。我不是专家