473,387 Members | 1,374 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,387 software developers and data experts.

static variables in derived classes

Hi.

I ahve three classes A,B,C, where B and C are derived from A.
A:>B,C.
Furthermore each class has an static int variable with name id,
static int id;
Besides that the class A implements the function
int GetId()
{
return id;
}

If I instanciate an object from the class B (where id is set to 1). The
function GetId() returns 0 (thats the value of the id in A).

Is there a way that GetId() delivers the right number, here 1, without
implementing GetId() in the classes B and C?

Thanks a lot
Nils


Jul 23 '05 #1
5 5212
Nils wrote:
Hi.

I ahve three classes A,B,C, where B and C are derived from A.
A:>B,C.
Furthermore each class has an static int variable with name id,
static int id;
Besides that the class A implements the function
int GetId()
{
return id;
}

If I instanciate an object from the class B (where id is set to 1). The
function GetId() returns 0 (thats the value of the id in A).

Is there a way that GetId() delivers the right number, here 1, without
implementing GetId() in the classes B and C?

Thanks a lot
Nils


A variable declared as static in a class means that
there is only one instance of that variable no matter
how many instances of the class there are.

In your case, there will only be one "id" variable
for every instance of class A, B, and C.

I would say to either remove the "static" from the
declaration, or create static variables in each
of the child classes. I can't give better advice
unless you give a description of what you are
trying to accomplish.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 23 '05 #2

"Nils" <sl***@web.de> wrote in message
news:cv***********@ariadne.rz.tu-clausthal.de...
Hi.

I ahve three classes A,B,C, where B and C are derived from A.
A:>B,C.
Furthermore each class has an static int variable with name id,
static int id;
Besides that the class A implements the function
int GetId()
{
return id;
}

If I instanciate an object from the class B (where id is set to 1). The
function GetId() returns 0 (thats the value of the id in A).

Is there a way that GetId() delivers the right number, here 1, without
implementing GetId() in the classes B and C?

Thanks a lot
Nils


I don't think it's a good idea to have a variable of the same name in
multiple classes in a class hierarchy. I'd probably design this whole thing
differently. How about just having a virtual function GetId(), which would
return a different value in each class? Why store the Id at all, especially
if it's static? Simply returning a numeric literal from a virtual function
would accomplish the same thing. (It's not *that* much extra work, after
all!)

It is also potentially an indication of poor design that you need to have a
unique identifier for each class type in the first place. How are you using
that information? If you're using it in a switch statement in order to
decide on some action, then the better alternative is usually to just use
virtual functions for the action(s), and let each class do what it needs to
do in its override of the virtual function.

-Howard


Jul 23 '05 #3
I don't think it's a good idea to have a variable of the same name in
multiple classes in a class hierarchy. I'd probably design this whole
thing differently. How about just having a virtual function GetId(),
which would return a different value in each class? Why store the Id at
all, especially if it's static? Simply returning a numeric literal from a
virtual function would accomplish the same thing. (It's not *that* much
extra work, after all!)

It is also potentially an indication of poor design that you need to have
a unique identifier for each class type in the first place. How are you
using that information? If you're using it in a switch statement in order
to decide on some action, then the better alternative is usually to just
use virtual functions for the action(s), and let each class do what it
needs to do in its override of the virtual function.

-Howard


I have done it with virutual functions now, almost like you suggested it.
Thanks!

Don't need it explicit for switch statements. the classes stands for
mathematical fuzzy-functions. The Id discribes some charakteristik of that
functions. I need it because not every operation is allowed to apply on
every function.

Thanks to all
Nils
Jul 23 '05 #4
Thomas Matthews wrote:
A variable declared as static in a class means that
there is only one instance of that variable no matter
how many instances of the class there are.

In your case, there will only be one "id" variable
for every instance of class A, B, and C.

Yes, I'm sure he understands that. The issue is that there
are three id variables, A::id, B::id, and C::id and he is
trying to differentiate between them. There's no such
thing as a virtual static nor a virtual data type so he'll
have to implement a virtual member function to what he's
describing.

Removing static from the declartion doesn't affect the
problem. The A::id, B::id, and C::id would still be
distinct in the C object.
Jul 23 '05 #5

"Ron Natalie" <ro*@sensor.com> schrieb im Newsbeitrag
news:42**********************@news.newshosting.com ...
Thomas Matthews wrote:
A variable declared as static in a class means that
there is only one instance of that variable no matter
how many instances of the class there are.

In your case, there will only be one "id" variable
for every instance of class A, B, and C.

Yes, I'm sure he understands that. The issue is that there
are three id variables, A::id, B::id, and C::id and he is
trying to differentiate between them. There's no such
thing as a virtual static nor a virtual data type so he'll
have to implement a virtual member function to what he's
describing.

Removing static from the declartion doesn't affect the
problem. The A::id, B::id, and C::id would still be
distinct in the C object.


Yes I wanted to say that exactly. Thanks :)
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Brad Kartchner | last post by:
I'm attempting to write a program with several child classes derived from a single parent class. The parent class has a couple of variables that need to be present in each of the child classes. ...
2
by: Earl Teigrob | last post by:
I am using C# to program ASP.NET. I am using class of static fields to hold all variables that are common to the application, and I am sure that this is fine. However, I was also using some static...
2
by: Mark | last post by:
I know that you can't have a static virtual property, but is there a way to simulate the same results? I have a base class that I want to extend so that you can change a value and it inherits...
6
by: ~~~ .NET Ed ~~~ | last post by:
Yes, I think so at least... In C# you *can* have static properties which are quite useful when used properly. Now imagine the scenario where you need the ability (sp?) to implement a variety of...
14
by: knocte | last post by:
Hello. I have a problem with C# language. I want to define an algorithm on a static function inside an abstract class. This function calls a static variable inside the same class. Then I want to...
6
by: MSDNAndi | last post by:
Hi, I have a baseclass (non-static) with some static and some non-static methods/fields/properties. In the baseclass in one of the static methods I need to do something like " somelogic...
12
by: Hemanth | last post by:
Hi, I have a base class with a static constructor and some abstract methods. Derived classes implement these methods. From articles on the web, it appears that there is no guarentee that this...
1
by: pasa_1 | last post by:
Are there any reasons one should avoid using static variables in abstract base classes? I was considering to the following: #include <iostream> using namespace std; enum { SZ = 10 };
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; };
2
by: cmonthenet | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.