一个关于 static的C++问题,求教各位大侠!
#include <iostream>
#include <string>
using namespace std;
class Base
{
public:
static void statmem();
};
class Derived : public Base
{
public:
void f(const Derived&);
} ;
void Derived::f(const Derived &obj)
{
Base::statmem();
Derived::statmem();
obj.statmem();
statmem();
}
int main()
{
return 0;
}
如果没有static就会出现 error C2662: 'statmem ' : cannot convert 'this ' pointer from 'const class Derived ' to 'class Base & ',为什么?
如果有static则出现error LNK2001: unresolved external symbol "public: static void __cdecl Base::statmem(void) " (?statmem@Base@@SAXXZ)
Debug/test5.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
怎么解决?
[解决办法]
楼主你首先要明白一个类的普通对象和const对象的区别,一个类的普通对象可以调用该类的任何成员函数;而const对象只能调用const成员函数以及静态成员函数,因为const就标志着告诉编译器,这个对象中的任何非static,非mutable数据成员不会被更改,而static函数保证这种调用的结果不会访问或者修改对象中的任何非static数据成员,const函数保证不会修改对象中的任何非mutable数据成员
一个关于 static的C++有关问题,求教各位大侠
一个关于 static的C++问题,求教各位大侠!#includeiostream#includestringusingnamespacestdclassBase
