- 论坛徽章:
- 0
|
c-v限定词和内联函数的问题
[quote]原帖由 "soloarrow"]这个const的作用是:用此函数访问时,不能修改对象(类)的成员属性,起到保护作用。[/quote 发表:
你说得没错,但是我不是问的这个问题。也许我在上面没有说清楚。
- #include <iostream.h>;
- class B
- {
- int ff;
- public:
- void fun1() const;
- void fun2();
- };
- void B::fun1() const
- {
- cout << "fun1" << endl;
- }
- void B::fun2()
- {
- cout << "fun2" << endl;
- }
- int main(int argc, char* argv[])
- {
- const B b1;
- B b2;
- b1.fun1();
- //! b1.fun2() // Illegal
- //! b2.fun1(); // Illegal
- b2.fun2();
- }
复制代码
在这里,const对象只能调用const成员函数。在看上面的例子,getFF()是一个const成员函数,却被一个非cosnt对象调用了。难道不矛盾吗?这里唯一的区别的就是getFF()是inline函数,但是我不知道为什么可以这样。 |
|