Inheritance with static constraint
HI.
Is there any way I can use inheritance in order to enforce my derived class use a static member?
Like:
Code:
class Base {
...
}
class Derived : public Base {
public:
static int _classNum;
}
So that every derived class would be enforced to use a static member?
I thought about pure virtual methods (getter/setter) in the Base class,
but they won't assure that the derived classes are defining the variable as static.
Is there a logic solution for that (I got it as an assignment, with this constraint)
Re: Inheritance with static constraint
Quote:
Originally Posted by
gmeir11
HI.
Is there any way I can use inheritance in order to enforce my derived class use a static member?
Like:
Code:
class Base {
...
}
class Derived : public Base {
public:
static int _classNum;
}
So that every derived class would be enforced to use a static member?
I thought about pure virtual methods (getter/setter) in the Base class,
but they won't assure that the derived classes are defining the variable as static.
Is there a logic solution for that (I got it as an assignment, with this constraint)
What exactly are you trying to do? :ehh:
Why must you enforce this staticness in your derived classes? Would defining the member inside a virtual method meet your needs?
Code:
class Base {
virtual const std::string& get_name() = 0;
}
class Derived : public Base {
public:
virtual const std::string& get_name();
}
const std::string& Base::get_name()
{
static const std::string name = "Base";
return name;
}
const std::string& Derived::get_name()
{
static const std::string name = "Base";
return name;
}
Re: Inheritance with static constraint
Quote:
Originally Posted by
gmeir11
I thought about pure virtual methods (getter/setter) in the Base class, but they won't assure that the derived classes are defining the variable as static.
This would be to enforce an implementation detail. Just define the pure virtual getter/setter methods in Base and specify their behaviour. Then it's up to any Derived class to make that come true. You can even hint that using a static variable probably is a good idea.
Re: Inheritance with static constraint
Would something like this be what you're looking for ?
Code:
template< typename T >
class base {
protected:
static T the_data ;
} ;
class derived : public base< int > {
// access the_data
} ;
Re: Inheritance with static constraint
As I understand it the class have that static variable,
so that all derived classes have access to that variable.
I need a way to enforce all derived classes to have a static variable each,
different variables, each for every derived class.
I believe there isn't, but can you think of a way to do that ?
Re: Inheritance with static constraint
You could use the Curiously Recurring Template pattern (note the slight modification of the above):
Code:
class base
{
...
};
template< typename T, typename Derived>
class base_CRTP: public base
{
protected:
static T the_data ;
} ;
class derived : public base_CRTP<int, derived>
{
// access the_data
} ;
Here the T template parameter is incidental; the magic is in requiring each derived class to inherit from base_CRTP<derived>. Each instantiation of this will be a different class, so therefor each derived type will have its own static member.