where to initialize a static class variable

L

lou zion

hello all,

i'm trying to wrap my head around this one, but something's missing. I'm
trying to create a class where all instances share a common variable that is
an instance of a different class. so, thought i'd use a static variable.
but, where can i initialize it?

myclass.h
-------------------
class MainClass()
{
declarations...

static OtherClass OC; // oc should be the same across all classes.
changes by one class instance change it in all instances
...
}

myclass.cpp
---------------------
#include myclass.h

//i want to initialize my static variable OC here so all classes can see it,
but how?

MainClass::MainClass(A1,A2,...): var1(A1), var2(A2)... // can't
initialize it here, cuz that's per instance, correct?
{
...
}

in a class module, when is code that is outside the class definition
executed? on the first instance? how and where can i initialize OC?

thanks!

lou
 
L

lou zion

myclass.h
-------------------
class MainClass()
{
declarations...

static OtherClass OC; // oc should be the same across all classes.
changes by one class instance change it in all instances
...
}

a new complication...

OtherClass is now MainClass. the idea is this: there is a default setting
for each variable in the class, but the defaults can be changed by the
class. i want every instance to have access to the current default settings
to retrieve them or change them. so, the above line now reads:

static MainClass OC; // OC should be the same across all classes.

thanks again!

lou
 
V

Victor Bazarov

lou said:
i'm trying to wrap my head around this one, but something's missing. I'm
trying to create a class where all instances share a common variable that is
an instance of a different class. so, thought i'd use a static variable.
but, where can i initialize it?

myclass.h
-------------------
class MainClass()
{
declarations...

static OtherClass OC; // oc should be the same across all classes.
changes by one class instance change it in all instances
...
}

myclass.cpp
---------------------
#include myclass.h

//i want to initialize my static variable OC here so all classes can see it,
but how?

#include otherclass.h

OtherClass MainClass::OC;
MainClass::MainClass(A1,A2,...): var1(A1), var2(A2)... // can't
initialize it here, cuz that's per instance, correct?
{
...
}

in a class module, when is code that is outside the class definition
executed?

Before 'main' function is called.
on the first instance?

Before it, unless your "first instance" is itself static. Then you
need to worry about the order in which they are initialised. Read
about "static object initialisation fiasco".
how and where can i initialize OC?

You initialise it as appropriate (according to OtherClass constructors)
somewhere in one of the translation units.

BTW, what C++ book are you reading that doesn't talk about static
data members and how they are initialised?

V
 
V

Victor Bazarov

lou said:
a new complication...

OtherClass is now MainClass. the idea is this: there is a default setting
for each variable in the class, but the defaults can be changed by the
class. i want every instance to have access to the current default settings
to retrieve them or change them. so, the above line now reads:

static MainClass OC; // OC should be the same across all classes.

This essentially changes only the class names in my response.

V
 
H

Howard

Victor Bazarov said:
This essentially changes only the class names in my response.

V

It changes a lot more than that, if I understand what he's saying. He's
said that OC is a member of MainClass. Then he said that, instead of OC
being of the type OtherClass, it's actually of the type MainClass. Well,
that's just not possible! You can't contain a class inside itself, even if
it's a static member, can you? The static member, after all, would now have
itself as a member! Or did I miss something?

Perhaps what is needed is just a single global object, instead of a static
member?

-Howard
 
J

Janusz Szpilewski

lou zion said:
hello all,

i'm trying to wrap my head around this one, but something's missing. I'm
trying to create a class where all instances share a common variable that
is an instance of a different class. so, thought i'd use a static
variable. but, where can i initialize it?

myclass.h
-------------------
class MainClass()
{
declarations...

static OtherClass OC; // oc should be the same across all classes.
changes by one class instance change it in all instances
...
}

myclass.cpp
---------------------
#include myclass.h

//i want to initialize my static variable OC here so all classes can see
it, but how?

Formally speaking you should define a static class data member anywhere in
the namespace enclosing the class definition and ensure the uniqueness of
the definition. Of course you should not do it in a header but it may be any
..cpp file which is compiled and linked with your project. Any other code
needs only to view a variable declaration which is done in your class
definition and included as a header file. Finally it is up to linker to
connect a static variable with any references to it in any compiled files.

Regards,
Janusz
 
V

Victor Bazarov

Howard said:
It changes a lot more than that, if I understand what he's saying. He's
said that OC is a member of MainClass. Then he said that, instead of OC
being of the type OtherClass, it's actually of the type MainClass. Well,
that's just not possible! You can't contain a class inside itself, even if
it's a static member, can you? The static member, after all, would now have
itself as a member! Or did I miss something?

Yes, you did miss the fact that 'OC' is a _static_ member. A static
data member is not "contained" inside "itself". It's a very good and
valid alternative to a global object.
Perhaps what is needed is just a single global object, instead of a static
member?

Perhaps you shouldn't be too hasty in your posting...

A static member that is an instance of the same class is one of relatively
common idioms. Imagine that I need a prototype from which all instances
are created unless some arguments are supplied to my constructor. Where
would I keep the "default values" for all members? Hard-coded? What if
I need to change them during run-time? I believe the OP needs exactly
that, some kind of "prototype".

V
 
H

Howard

Victor Bazarov said:
Yes, you did miss the fact that 'OC' is a _static_ member. A static
data member is not "contained" inside "itself". It's a very good and
valid alternative to a global object.


Perhaps you shouldn't be too hasty in your posting...

Not hasty, just ignorant in this case, it seems....
A static member that is an instance of the same class is one of relatively
common idioms. Imagine that I need a prototype from which all instances
are created unless some arguments are supplied to my constructor. Where
would I keep the "default values" for all members? Hard-coded? What if
I need to change them during run-time? I believe the OP needs exactly
that, some kind of "prototype".

Cool. I like that you can do that. I just never heard of such a thing. I
guess it's possible because the static members aren't really stored inside
the object, so that even that static instance itself can "refer" to the
static instance (itself). But since OC is now a member of MainClass,
wouldn't that allow for something like this..?

MainClass x;
x.OC.OC.OC.OC.OC.OC.foo(); // as many .OC as desired?

[running off and testing...]

Well, blow me down! That's exactly what happens! :) The following code
works (and prints 3, as expected):

class MC
{
public:
MC(int initX = 0) : x(initX) {};

static MC mc;
int x;
};

MC MC::mc;

int main(int argc, char* argv[])
{
char temp[10];

MC a;
a.mc.mc.mc.mc..mc.mc.mc.mc.mc.mc.x = 3;
std::cout << a.mc.x << std::endl;
std::cin >> temp;

return 0;
}


That's gotta be the strangest code I've ever written, at least that actually
worked! :)

Thanks for the info, Voctor!

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top