Inheriting and static members

Posted by Bruce on Stack Overflow See other posts from Stack Overflow or by Bruce
Published on 2010-05-20T06:59:48Z Indexed on 2010/05/20 7:30 UTC
Read the original article Hit count: 289

Filed under:

Here is my code -

#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;
}

I get a linker error here. But when I do it the other way round -

class Base
{
public:
int static a;

};

int Base::a = 5;

class Derived : public Base
{

public:
int a;
};

I get no error. Can someone please explain what is happening here.

© Stack Overflow or respective owner

Related posts about c++