|
-
September 16th, 2010, 06:39 PM
#1
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)
-
September 17th, 2010, 02:18 AM
#2
Re: Inheritance with static constraint
 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? 
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;
}
Last edited by monarch_dodra; September 17th, 2010 at 02:20 AM.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
September 17th, 2010, 05:49 AM
#3
Re: Inheritance with static constraint
 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.
-
September 17th, 2010, 05:58 AM
#4
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
} ;
your humble savant
-
September 17th, 2010, 09:04 AM
#5
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 ?
-
September 17th, 2010, 09:12 AM
#6
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|