c – 为什么类的静态成员函数没有“常量正确性”的概念?
发布时间:2020-12-16 07:51:47 所属栏目:百科 来源:网络整理
导读:用例: class A { static int s_common;public: static int getCommon () const { s_common; };}; 通常会导致以下错误: error: static member function ‘static int A::getCommon()’ cannot have cv-qualifier 这是因为const只适用于指向该对象的对象,静
用例:
class A { static int s_common; public: static int getCommon () const { s_common; }; }; 通常会导致以下错误:
这是因为const只适用于指向该对象的对象,静态成员函数中不存在该对象. 但是如果允许,静态成员函数的“const”可能与静态数据成员很容易相关. 解决方法
cv-qualifiers影响函数的签名.所以你可以有:
class A { static int s_common; public: static void getCommon () const { }; static void getCommon () { }; }; 现在…你会如何调用const?没有const对象可以调用它(嗯,你可以调用它在一个const对象上,但这不是重点). 我只是在这里猜测,可能还有其他原因. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |