473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Static Member Variables + Inheritance

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Michael Klatt | last post by:
I am working on a library which uses the GKS graphics package. I have a Gks object which opens the GKS subsystem in its constructor: // Gks.hpp class Gks { public : Gks(); };
10
by: cppaddict | last post by:
Hi, Say I have a abstract base class and I know that every derived class will have a static data member (call it SharedInformation) representing information to be shared across all instances of...
7
by: Sunny | last post by:
Hi all, According C# Language Specification : 10.11 Static constructors: The static constructor for a class executes at most once in a given application domain. The execution of a static...
4
by: Bryan Green | last post by:
So I'm working on a project for a C# class I'm taking, where I need to keep some running totals via static variables. I need three classes for three different types of objects. The base class and...
6
by: Bill Rubin | last post by:
The following code snippet shows that VC++ 7.1 correctly compiles a static member function invocation from an Unrelated class, since this static member function is public. I expected to compile the...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
4
by: Chukkalove | last post by:
Hi I have an abstract class that contains 100% static methods and variables. One of the member variables "string DatabaseName" needs to be overridden in derived classes. Am I able to keep my...
8
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
13
by: learning | last post by:
Hi I have a static class written by other team which encapsulates a database instance. but I need to extend it to incldue other things. I know that C# static class is sealed and can;t be inherited...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.