473,320 Members | 1,828 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,320 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 5209
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.