2

I have a class that has a static pointer to another class, which is a singleton. The problem that I seem to be getting though is that I cannot set the pointer from within the constructor of said class. Here is my current code:

class B;

class A
{
  public:
    A();
    ~A();
};


class B
{
  public:

    B();
    ~B();

    static A* a;
};

A::A() {
  A* B::a = this;
}

Now, the problem that I seem to be getting is that I cannot define B::a in the "current scope". In addition, the "this" pointer can only be used in a "nonstatic member function".

I think the problem has to do with defining the static class variable within another class function, but I am not sure.

Jack
  • 125,196
  • 27
  • 216
  • 324
  • So you want a static member that can be used before the class is instantiated to not be defined until another class is instantiated? – chris Oct 29 '12 at 03:37
  • What are you asking? I am not sure what you mean. – user1703993 Oct 29 '12 at 03:39
  • Since `B::a` is public (in addition to being static), you can do `B::a = this;` in the constructor of `A`. But the effect will be that 1) `B::a` won't be initialised until the first intance of `A` is created, and 2) Every time a new instance of `A` is created, `B::a` will change. Are you sure that is what you want? It sounds extremely unusual. – jogojapan Oct 29 '12 at 03:43
  • A is a singleton class, meaning it will only be constructed once. It will also be constructed in the beginning of the program, so virtually nothing occurs prior to it. – user1703993 Oct 29 '12 at 03:46
  • Ok. I would find it a lot more intuitive if `B::a` was assigned its value in the same place where the one-and-only instance of `A` is created, rather than from inside `A`'s constructor, but it's certainly possible. The key problem in your code is not the assignment `B::a = this`, but the fact that there is `A *` in front of it, which makes it a declaration, rather than an assignment. – jogojapan Oct 29 '12 at 03:49
  • Yeah. I thought that that was necessary from other relevant questions, but I didn't know that it had to be separate from the assignment. – user1703993 Oct 29 '12 at 03:51

2 Answers2

2

First of all, static class members have to be defined as well as declared. static A* a; in the body of class B is merely a declaration, and you need to define a. Note that every class member must have a single definition, that's why we usually define static members in the appropriate .cpp files for classes. Obviously, this happens outside of the class body. You also cannot define a static member in scope of a function.

So, move the definition of a outside of the constructor's scope, like this:

class B
{
public:

    B();
    ~B();

    static A* a;};

A* B::a = 0; // Good idea to set a default value for the pointer

Note, however, that the value of a will contain a 0 until at least one object of type A is created.

  • Okay, I get it. I didn't know you had to define the static variable twice. – user1703993 Oct 29 '12 at 03:49
  • Note that you actually *define* a static variable only once. The answers to this question http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration will help you better understand the difference between declaration and definition :) –  Oct 29 '12 at 03:53
0

You should define all static members outside the class declaration:

class B;
class A
{
public:
    A();
    ~A();
};


class B
{
public:

    B();
    ~B();

    static A* a;
};

A* B::a; // HERE

A::A()
{
    B::a = this; // and there was an error here too
}

Don't forget to define the other ctors/dtors.

Murilo Vasconcelos
  • 4,329
  • 20
  • 27