Static Member Variables + Inheritance

M

mike

Ok,
If I have the following code:


//main.cpp

class Base
{
public:
int static a;

};

int Base::a = 0;

class D1 : public Base
{
const b;
public:
D1() : b(a) {}
};

so on the construction of a D1 object, i intend it to store the value
of a. The code compiles and seems to be running fine I am just a bit
suspect! Is it ok to code like this??
Regards

Mike
 
S

Sandeep

mike said:
Ok,
If I have the following code:


//main.cpp

class Base
{
public:
int static a;

};

int Base::a = 0;

class D1 : public Base
{
const b;
public:
D1() : b(a) {}
};

so on the construction of a D1 object, i intend it to store the value
of a. The code compiles and seems to be running fine I am just a bit
suspect! Is it ok to code like this??
Regards

Mike

I am not sure which part of the code you are suspecting. can you be
more specific ?
But if it about the initialization of static "a" ( int Base::a = 0;)
you are concerned, I think that _is_ the way to handle static variables
in the class.
 
S

Sandeep

mike said:
Ok,
If I have the following code:


//main.cpp

class Base
{
public:
int static a;

};

int Base::a = 0;

class D1 : public Base
{
const b;
public:
D1() : b(a) {}
};

so on the construction of a D1 object, i intend it to store the value
of a. The code compiles and seems to be running fine I am just a bit
suspect! Is it ok to code like this??
Regards

Mike

If you are concerned about initialization of "b" ( i am assuming you
meant "const int b"), again it is fine. Since it is a "const" class
variable it _needs_ to be initialized in the constructor.
 
V

Valentin Samko

mike said:
class Base
{
public:
int static a;

};

int Base::a = 0;

class D1 : public Base
{
const b;
public:
D1() : b(a) {}
};

so on the construction of a D1 object, i intend it to store the value
of a. The code compiles and seems to be running fine I am just a bit
suspect! Is it ok to code like this??

Yes, as 'a' is already defined when you create objects of class D1, see 9.4.2/3.
 
V

Valentin Samko

mike said:
class Base
{
public:
int static a;

};

int Base::a = 0;

class D1 : public Base
{
const b;
public:
D1() : b(a) {}
};

so on the construction of a D1 object, i intend it to store the value
of a. The code compiles and seems to be running fine I am just a bit
suspect! Is it ok to code like this??
You also forgot to specify the type of 'b'. I assume this is a misprint.
 
M

mike

Thanks Guys, yeas it should have read "const int b"
I just had it in the back of my mind that using static variables and
inheritance was a dangerous thing to do? Is it still ok if i split up
my code into differnent files??
Mike
 
G

Greg Comeau

Thanks Guys, yeas it should have read "const int b"
I just had it in the back of my mind that using static variables and
inheritance was a dangerous thing to do? Is it still ok if i split up
my code into differnent files??

I agree it seems that the logistics of your function is safe.
However, it seems a quite backdoorish way to snapshot the value
that way. IOWs, you may want some interface functions to complement
the static variable.
 
M

mike

The problem i have is a tree structure, with different nodes derived
from the the same base class, each with variable number of children
(also from base class) and functionality, but the tree needs to 'step
through time', an create new children, which record the time they were
created. I though about passing the time variable from the top of the
tree to all the children recursively, but it seems like a lot of of
additional typing....., so i thought this solution was cleanest!
By interface functions do you mean get/set func's?
Many thanks

Mike
 
S

Sandeep

mike said:
The problem i have is a tree structure, with different nodes derived
from the the same base class, each with variable number of children
(also from base class) and functionality, but the tree needs to 'step
through time', an create new children, which record the time they were
created. I though about passing the time variable from the top of the
tree to all the children recursively, but it seems like a lot of of
additional typing.....,so i thought this solution was cleanest!
By interface functions do you mean get/set func's?
Many thanks

Mike

Another alternative for what you are doing is to have a simple class
that will have a static member to hold this value for you.

class TimeCreate
{
static int timeCreated;
};
int TimeCreate::timeCreated = 0;

It is a matter of design choice that could make your code less
suspect.This class can be used to hold other static variables as well.
 

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

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,666
Latest member
selsetu

Latest Threads

Top