Archived

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

Krylloan

C++ OO Question: Best way to...

Recommended Posts

What is the best way to code a class whose derived classes have static variables. (Basically, a virtual-static variable/constant) for example: class animal { static virtual int no_of_feet; } class cow : public animal { static int no_of_feet = 4; } class human : public animal { static int no_of_feet = 2; } The only ways I can think of are: 1) ------------------------------------- struct animal_type { int no_of_feet; } animal_type cow_type = { 4 }; class animal { animal_type *type; } class cow : public animal { cow(); } cow::cow() { type = &cow_type; } ---------------------------------------- ( Annoying to code ) and using a function 2)-------------------------------------- class animal { virtual int no_of_feet(); } // which requires each animal to have the code below int cow::no_of_feet() { return 4 }; ---------------------------------------- ( Slow to run, slow to code ) (or you could just set the variabe no_of_feet for each instance of cow, human or whatever, on construction, but that''s just dumb) I''m having this problem with many things - mainly widget-types, input-devices, and weapon-types. I''m currently using method 1. Is there a better way? does C++ support this, even? Any ideas? thanks Simon Hill

Share this post


Link to post
Share on other sites
How about:


        
template <int N>
class animal
{
public:
const int no_of_feet;
animal() : no_of_feet(N)
{
}

};

class cow : public animal<4>
{
};

class human : public animal<2>
{
};


Probably not an ideal solution either, and you can't use it with statics, but it works... (it is actually just a different way of initialising it with the constructor, but it forces you to define the number of legs for each animal you create - if you forget it simply won't compile)

Basically, there is no way to do what you are trying to do with static variables. You can't have a 'virtual' static for reasons which should be obvious when looking at the syntax for accessing static variables. You have to do it via member variables or take the speed hit of using virtual accessors.



[edited by - Sandman on May 1, 2002 8:17:25 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Sandman
How about:
[... stuff ...]

That is not what the OP desires, as cow and human are deriving from two different base classes.

Krylloan: you should do the simplest thing that works.

Personally, I would probably use no 2. (with the proviso that we are not talking about a real world example here). I'd make the no_of_feet() function pure virtual in the base class, thus requiring each concrete derived class to implement the function. Don't worry about whether it's slow to run: you don't know that yet. And don't worry about slow to code, either. How many derived classes are you expecting to write? If it was a thousand, I might understand your concern over typing extra characters, but I'd also say you have a more serious problem in your design. Sometimes, a little extra typing now can save a lot of bug-hunting in the future.

Edit: and don't forget the virtual dtor in the base class!


[ C++ FAQ Lite | ACCU | Boost | Python | Agile Manifesto! ]

[edited by - SabreMan on May 1, 2002 8:50:26 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
That is not what the OP desires, as cow and human are deriving from two different base classes.



Good point, I think my brain must have been half asleep because that didn''t occur to me as I was writing it. Duh.

I agree that using the accessors is probably the best approach though.



Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Just go with method 2 with no_of_feet() being pure virtual in animal like SabreMan said. Method 1 just looks horrible. I''m pretty sure speed won''t be an issue.

Share this post


Link to post
Share on other sites