Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

billybob

static class members and derived classes

This topic is 5737 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

how can i make it so there is a different variable per derived class, but the same for each instance of a particular class, like its the same for all of class A, but class B derived from A has a different value?

Share this post


Link to post
Share on other sites
Advertisement
Use a different static variable for the deriving class? If it's one value per class, but different for the deriving classes, then they probably shouldn't be the same variable. Can you be more specific about why you want to do this?

edit: in MSVC you can do this (don't know if it is standard c++)

class Super {
private:
static int bla1;
public:
static int getBla() {
return bla1;
}
}

int Super::bla1=1;

class Derived: public Super {
private:
static int bla2;
public:
static int getBla() {
return bla2;
}
}

int Derived::bla2=2;

These static members cannot be virtual, so there will be no polymorphism (don't even know how that would be syntax-like).

[edited by - Koen on January 30, 2003 10:27:47 PM]

Share this post


Link to post
Share on other sites
Well, if i'm not mistaken, a static member of a class is shared through all instances and derivations of a class (no matter what the value)...

Just change the value in the constructor of the derived class, but make sure that the value is protected, and not private in the base class. Then make the constructor virtual...


    
class SuperFreak
{
public:
virtual SuperFreak();
protected:
int SomeVariable;
}
//Constructor for SuperFreak

SuperFreak::SuperFreak() //shes super freakay!

{
SomeVariable = 10;
}

class BrickHouse : public SuperFreak
{
public:
BrickHouse();
}
//Constructor for BrickHouse

BrickHouse::BrickHouse()
{
SomeVariable = 1;
}


When you create a BrickHouse object, it derives the SomeVariable, but not the value (essentially. What happens is that the SuperFreak constructor is called, then the BrickHouse constructor is called. So you forst set the value of SomeVariable to 10, then to 1. When the objects are destroyed, it is in reverse order.)

does this help you?



[edited by - MrBeaner on January 30, 2003 12:13:14 AM]

Share this post


Link to post
Share on other sites
not really, the only real reason i need it static is so i can get a few variables about it without having an instance. its the base game object class for my game, there are 3 variables that i need to get, parent type, type, and description. i need to get these without having an instance of the class in my editor, so you can see the description and stuff before you place it.

Share this post


Link to post
Share on other sites
If you need a static, you just redeclare it.


  
class A
{
static int I;
public:
virtual ~A() {} // class is used for public derivation.

};

class B : public A
{
static int I;
};

int A::i = 0;
int B::i = 1;


If you don''t want it static, just re-initialise it accordingly in B''s constructor.


  
class A
{
int I;
A() : I( 0 ) {}
virtual ~A() {} // class is used for public derivation.

};

class B : public A
{
B() : I( 1 ) {}
};




[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!