Static variables in classes

If I have a static variable in a class e.g. a pointer to another class like this: (B is another class)
1
2
3
4
5
class A
{
public:
   static B* cB;
};

Then I set that variable and create multiple instances of class A like this:
1
2
A::cB = new B;
As = new A[Number];


Then will the value of cB be the same across all instances?

I cannot pass the class pointer in the constructor as I need to create an array of instances. I tried this method but I get linker error.... unresolved external.
Last edited on
Static data members are not associated with any specific instance.

You don't need a static variable like this. For one, you can call a different constructor than the default constructor when you make an array, and for two, you should be using std::vector.
1
2
3
4
5
std::vector<A> MyVec;
for(blah)
{
    MyVec.push_back(A{B{}});
}
Yeah but in the default constructor it needs the pointer to another class... so I have to somehow set that static variable before I create the instances.

Yeah I guess I could use a vector.
Last edited on
Don't use a static variable.
Then how do I pass the class pointer to the other class before I create the instance?
You use the constructor:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <vector>
#include <algorithm>

struct B
{
    int blah;
    void Blah()
    {
        std::cout << blah << std::endl;
    }
};

struct A
{
    B *bp;
    A(B *youmakethewayalittlebetter) : bp(youmakethewayalittlebetter)
    {
    }
    void Meh()
    {
        bp->Blah();
    }
};

int main()
{
    B b {7};
    std::vector<A> av (10, &b);
    std::for_each(av.begin(), av.end(), [](A &a)
    {
        a.Meh();
    });
}
http://ideone.com/pmFBVo
But can I do it with a static variable? Because I could be creating a large number of instances, and having a static variable could save a bit of memory. And IMO it would be easier.
No, you cannot do it with a static variable. There would be no way to have different 'groups' of A, each group pointing to a different B.
I don't know if you really understand what I want.

I want to set a static variable in a class at runtime before I create an instance of that class. How can I do this?

So pretty much, this is what I want:
1
2
CLASS::StaticPointer = value;
CLASS *a = new CLASS[numberofclasses];

But I get an unresolved external....

Or the question could be rephrased as:
How can I set a static variable in a class during runtime?
Last edited on
Your question is not "How can I set a static variable in a class during runtime", it is "Why do I get an unresolved external symbol error when I try to link my program?"

You need to provide a definition for the static variable in one and only one cpp file (because static variables are basically globals in a class scope)
1
2
3
4
5
//MyClass.hpp
struct MyClass
{
    static int MyGlobal; //declaration
};
1
2
3
4
//MyClass.cpp
#include "MyClass.hpp"

int MyClass::MyGlobal {0}; //definition - required 
1
2
3
4
//Main.cpp
#include "MyClass.hpp"

//... 
Last edited on
Ok, working now.

Thanks so much.
Just note that if you change the static variable later it will affect the instances you already created, which not what you want :p
Yup, thanks for the warning. I won't be changing it :D
Topic archived. No new replies allowed.