这里是我的代码 -继承和静态变量
#include <iostream>
#include <conio.h>
using namespace std;
class Base
{
public:
int a;
};
//int Base::a = 5;
class Derived : public Base
{
public:
int static a;
};
int main()
{
Derived d;
cout<<d.a;
getch();
return 0;
}
我在这里得到一个连接错误。但当我这样做时,相反 -
class Base
{
public:
int static a;
};
int Base::a = 5;
class Derived : public Base
{
public:
int a;
};
我没有得到任何错误。有人能解释一下这里发生了什么吗?
没有答案,但为什么你的事件想要2个对象相同的变量名。只是添加混淆(即使对链接器) – RvdK 2010-05-20 07:03:12
@PoweRoy:链接器不会在意 - 它使用装饰名称。 – sharptooth 2010-05-20 07:04:10
@PoweRoy:没有理由只是修修补补周围 – Bruce 2010-05-20 07:07:11