Overriding base class

J

James Emil Avery

Hi all,

I have a problem where

1. I have many derived classes of a base class.
2. The base class has many functions implementing common behaviour.
3. The code should be the same for all the derived classes, but the
unctions depend on *static, constant data* that is specific to each
erived class.
4. I cannot declare this static data virtual, because I must also be able
to instantiate the base class; I.e. I must have the static data in the
base class as a fall-back.

My problem: I override the data in the derived classes, but the common
functions still use the data from the base class.

I do not wish to duplicate the base-class functions to all the many
derived classes, because of the unnecessary blow-up of code size,
decreased readability and maintenance. I also want to keep the data
static, since memory requirements would otherwise drastically increase.

Is there any way in C++ that makes this setup possible? I.e. have code
that is shared between all derived classes, but which depends on data that
is specific to each derived class?

Thanks very much in advance.
 
M

Michael DOUBEZ

James Emil Avery a écrit :
Hi all,

I have a problem where

1. I have many derived classes of a base class.
2. The base class has many functions implementing common behaviour.
3. The code should be the same for all the derived classes, but the
unctions depend on *static, constant data* that is specific to each
erived class.
4. I cannot declare this static data virtual, because I must also be able
to instantiate the base class; I.e. I must have the static data in the
base class as a fall-back.

My problem: I override the data in the derived classes, but the common
functions still use the data from the base class.

I do not wish to duplicate the base-class functions to all the many
derived classes, because of the unnecessary blow-up of code size,
decreased readability and maintenance. I also want to keep the data
static, since memory requirements would otherwise drastically increase.

Is there any way in C++ that makes this setup possible? I.e. have code
that is shared between all derived classes, but which depends on data
that is specific to each derived class?

That looks like a job for the "Curiously recurring template pattern"
(CRTP). There is an extensive litteratur about it, just google for it.

Here is an example:
template <class Derived>
struct base
{
int value()
{
return Derived::value;
}

//other function to factor out
// as a bonus, you can call derived function
// static_cast<Derived*>(this)->implementation();
// this is static polymorphism
};

struct derived : base<derived>
{
enum{value=1};
};

int main()
{
derived a;

std::cout<<a.value()<<std::endl;

return 0;
}

Michael
 
M

Michael DOUBEZ

Michael DOUBEZ a écrit :
James Emil Avery a écrit :

That looks like a job for the "Curiously recurring template pattern"
(CRTP). There is an extensive litteratur about it, just google for it.

Here is an example:
template <class Derived>
struct base
{
int value()
{
return Derived::value;
}

//other function to factor out
// as a bonus, you can call derived function
// static_cast<Derived*>(this)->implementation();
// this is static polymorphism
};

struct derived : base<derived>
{
enum{value=1};
};

int main()
{
derived a;

std::cout<<a.value()<<std::endl;

return 0;
}

Oups, conflicting names here, use another function name.

Michael
 
J

James Emil Avery

James Emil Avery a écrit :

That looks like a job for the "Curiously recurring template pattern" (CRTP).
There is an extensive litteratur about it, just google for it.
[Example]

Thanks a lot for the prompt response! It looks like a good fit; I'll
immediately have a go at molding my problem into it. Thanks!
 
T

tony_in_da_uk

3. The code should be the same for all the derived classes, but the
unctions depend on *static, constant data* that is specific to each
erived class.
4. I cannot declare this static data virtual, because I must also be able
to instantiate the base class; I.e. I must have the static data in the
base class as a fall-back.

My problem: I override the data in the derived classes, but the common
functions still use the data from the base class.

Very easy and "proper" solution: access the data through virtual
functions...

class Base { virtual X get_x() { return my_static_x; } };

class Derived1 { virtual X get_x() { return my_own_static_x; } };
class Derived2 { /* use base classes get_x */ };

Cheers,

Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top