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 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
"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 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
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.
"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 :) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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. ...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 };
|
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;
};
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| |