-
Notifications
You must be signed in to change notification settings - Fork 8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
常对象,只能调用常成员函数、更新常成员变量? #41
Comments
当常成员变量是指针的时候可以更新,如 #12 (comment) 中的 c |
请问常成员变量是“常量指针”还是“常量指针”?(c应该是常量指针吧?) |
我大致明白了,您说的“常成员变量”应该是“常量指针”? |
我说的 但刚刚测试了一下,仓库中的例子 a ( #include <iostream>
using namespace std;
class A
{
public:
int a1;
const int a2;
const int* a3;
A(int _i1, int _i2) :a1(_i1), a2(_i2), a3(&a1) {}
void fun1() { a3 = &a2; std::cout << "fun1()" << std::endl; }
void fun2() const { std::cout << "fun2() const" << std::endl; }
};
int main()
{
A b(1, 2);
b.a3 = &b.a2; // 可以更新常成员变量
std::cout << *b.a3 << std::endl;
const A a(3, 4);
a.fun1(); // 编译出错,不可以调用普通成员函数
a.fun2(); // 可以调用常成员函数
a.a3 = &a.a2; // 编译出错,不可更新常成员变量
return 0;
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
请问常成员变量为什么要被更新?
The text was updated successfully, but these errors were encountered: