By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,507 Members | 1,758 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,507 IT Pros & Developers. It's quick & easy.

Static Member Variables + Inheritance

P: n/a
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

Nov 22 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
mike wrote:
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.

Nov 22 '05 #2

P: n/a
mike wrote:
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.

Nov 22 '05 #3

P: n/a
mike wrote:
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.

--

Valentin Samko - http://www.valentinsamko.com
Nov 22 '05 #4

P: n/a
mike wrote:
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.

--

Valentin Samko - http://www.valentinsamko.com
Nov 22 '05 #5

P: n/a
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

Nov 22 '05 #6

P: n/a
In article <11*********************@g14g2000cwa.googlegroups. com>,
mike <sl***********@hotmail.com> wrote:
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.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 22 '05 #7

P: n/a
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

Nov 22 '05 #8

P: n/a

mike wrote:
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.

Nov 22 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.