#include <iostream>
using namespace std;
class C
{
private:
int x;
public:
C(int x){this->x = x;}
int getX()const{return x;}
};
void main()
{
const C c(5);
cout<<c.getX();
system("pause");
}
#include <iostream>
using namespace std;
class C
{
private:
int x;
public:
C(int x){this->x = x;}
int getX(){return x;}
};
void main()
{
C c(5);
cout<<c.getX();
system("pause");
}
出错原因是常变量成员只能用常对象来调用
我认为第一种好,运用了长对象成员的调用
本文通过两个C++代码示例对比展示了常对象如何调用成员函数,并解释了为何必须使用const限定符声明对象及成员函数。文章强调了正确使用const的重要性。
&spm=1001.2101.3001.5002&articleId=7396252&d=1&t=3&u=1edc0e9391014a9cb6c35def2c8eb9a3)
236

被折叠的 条评论
为什么被折叠?



