How To Initialize static data members?

I'm getting another error when I try to compile. It's telling me I must initialize my static data member but when I try to do that it tells my only constants may be initialized within the class. I'm using an abstract class in a header file so I don't want to have to make a source file just for one static data member, is there any work around?
Inside a class static data members are only declared but not defined. You shall define a static data member.

For example

In a header

struct A
{
static int x;
};

in a cpp-file

int A::x = 10;

You may initilaize only const static data members inside a class.
Last edited on
A possible workaround is this:

1
2
3
4
5
6
7
8
struct A
{
  static Foo& foo()
  {
    static Foo foo;
    return foo;
  }
};

@Athar
A possible workaround is this:

1
2
3
4
5
6
7
8
struct A
{
  static Foo& foo()
  {
    static Foo foo;
    return foo;
  }
}; 



It is invalid code becuase Foo is undefined name.
I can't tell whether you're joking or not.
closed account (o1vk4iN6)
god forbid if you need to think for yourself a little bit.
Last edited on
Topic archived. No new replies allowed.