taz
taz

Reputation: 1526

Static member in base class is null when assigned in derived class

I have a base class with a static pointer member. When I assign that static pointer member in a derived class, that static member appears NULL when referenced from the methods of the base class.

This is not the behavior I expect. Shouldn't the static pointer member still be assigned regardless of where it is accessed from? Am I wrong about the expected behavior?

The static member is a pointer to a base class. I realize that what I am trying to achieve is probably best accomplished by static polymorphism using templates ( Static polymorphism definition and implementation ) but I still do not understand why in this scenario the pointer member itself is NULL.

Edit: the behavior is NOT demonstrated in this SSCE. What bugs or common errors would cause the behavior I describe? My actual production scenario is significantly more complicated than this, but the structure is almost exactly the same. I am literally in the VS2010 debugger observing Base::staticMember == not null, then call to Base::staticMethod(), and inside Base::staticMethod(), Base::staticMember is null. I am baffled.

#include <iostream>

class Base {
public:
    static Base *staticMember;

    static void baseMethod();
};

Base *Base::staticMember = NULL;

class Derived : public Base {
public:
    void derivedMethod();
};

void Base::baseMethod() {
    // here staticMember == NULL, the unexpected behavior
    if(staticMember == NULL) {
        std::cout << "NULL";
    } else {
        std::cout << "NOT NULL";
    }
};

void Derived::derivedMethod() {
    staticMember = new Derived();
}

int main(int argc, void *argv[]) {
    Derived *derived = new Derived();
    derived->derivedMethod();
    Base::baseMethod();
}

Upvotes: 0

Views: 1045

Answers (4)

brian beuning
brian beuning

Reputation: 2862

Maybe you have accidentally declared class Derived { static Base *staticMember; };

So you have two staticMembers floating around.

This is why an SSCE is useful, to keep people like me from making unfounded guesses.

Upvotes: 0

Zero
Zero

Reputation: 12109

As mentioned, the SSCE works, but your prod code doesn't. If you make a change like:

void Derived::derivedMethod() 
{
    Base::staticMember = new Derived();
}

this explicitly tells the compiler what variable you are talking about, and may throw up cases where you are accidentally hiding the base member.

(I think gcc might require Base::staticMember - perhaps not in this exact case)

Upvotes: 0

GManNickG
GManNickG

Reputation: 504333

No repro on a fixed up and simplified version of your code:

#include <iostream>
#include <iomanip>

struct Base {
    static Base* staticMember;

    static void baseMethod();
};

Base* Base::staticMember;

void Base::baseMethod() {
    std::cout << std::boolalpha << (staticMember == nullptr) << std::endl;
}

struct Derived : Base {
    void derivedMethod();
};

void Derived::derivedMethod() {
    staticMember = this;
}

int main() {
    Derived derived;
    derived.derivedMethod();

    Base::baseMethod();
}

Prints "false".

Always give a Short, Self Contained, Correct (Compilable), Example, or your question is meaningless.

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258698

Your compiler is clearly the problem here, and I'm not surprised since it allows both

int main(int argc, void *argv[])

and

Derived derived = new Derived();

For example, see the result in ideone. The static member should clearly not be null.

Upvotes: 2

Related Questions