Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   where to initialize a static class variable (http://www.velocityreviews.com/forums/t291635-where-to-initialize-a-static-class-variable.html)

lou zion 06-09-2005 06:16 PM

where to initialize a static class variable
 
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










lou zion 06-09-2005 06:33 PM

Re: where to initialize a static class variable
 
> 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



Victor Bazarov 06-09-2005 07:37 PM

Re: where to initialize a static class variable
 
lou zion wrote:
> 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

Victor Bazarov 06-09-2005 07:38 PM

Re: where to initialize a static class variable
 
lou zion wrote:
>>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.


This essentially changes only the class names in my response.

V

Howard 06-09-2005 07:46 PM

Re: where to initialize a static class variable
 

"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:qU0qe.82052$NC6.2683@newsread1.mlpsca01.us.to .verio.net...
> lou zion wrote:
>>>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.

>
> 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




Janusz Szpilewski 06-09-2005 07:48 PM

Re: where to initialize a static class variable
 
"lou zion" <illuzioner@adelphia.net> wrote in message
news:CcCdnQPoY80QCDXfRVn-gA@adelphia.com...
> 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



Victor Bazarov 06-09-2005 07:49 PM

Re: where to initialize a static class variable
 
Howard wrote:
> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
> news:qU0qe.82052$NC6.2683@newsread1.mlpsca01.us.to .verio.net...
>
>>lou zion wrote:
>>
>>>>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.

>>
>>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?


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

Howard 06-09-2005 09:15 PM

Re: where to initialize a static class variable
 

"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:x31qe.82054$NC6.75652@newsread1.mlpsca01.us.t o.verio.net...
> Howard wrote:
>> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
>> news:qU0qe.82052$NC6.2683@newsread1.mlpsca01.us.to .verio.net...
>>
>>>lou zion wrote:
>>>
>>>>>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.
>>>
>>>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?

>
> 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...


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







Howard 06-09-2005 09:18 PM

Re: where to initialize a static class variable
 

"Howard" <alicebt@hotmail.com> wrote in message
news:xj2qe.923485$w62.100715@bgtnsc05-

> a.mc.mc.mc.mc..mc.mc.mc.mc.mc.mc.x = 3;


fat-fingered that one. change that .. to just . and it works.




All times are GMT. The time now is 11:16 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.