473,404 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Overriding base class


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.

--
Med venlig hilsen,
James Avery <av***@diku.dk>
Jul 25 '07 #1
4 1724
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
Jul 25 '07 #2
Michael DOUBEZ a écrit :
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;
}
Oups, conflicting names here, use another function name.

Michael
Jul 25 '07 #3
On Wed, 25 Jul 2007, Michael DOUBEZ wrote:
James Emil Avery a écrit :
>>
Hi all,

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.
[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!
--
Med venlig hilsen,
James Avery
Jul 25 '07 #4
On Jul 25, 11:32 pm, James Emil Avery <av...@diku.dkwrote:
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

Jul 26 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ali Eghtebas | last post by:
Hi, I have 3 questions regarding the code below: 1) Why can't I trap the KEYDOWN while I can trap KEYUP? 2) Is it correct that I use Return True within the IF-Statement? (I've already read...
8
by: Edward Diener | last post by:
Is it possible for a derived class to override a property and/or event of its base class ?
5
by: Hongzheng Wang | last post by:
Hi, I have a problem about the overriding of private methods of base class. That is, if a method f() of base class is private, can the derived class overriding f() be overriding? For...
8
by: Massimiliano Alberti | last post by:
Can I specialize a template function in a subclass without overriding it? (the main template function is defined in a base class). Now I'm doing something like that: (in base class)...
15
by: Susan Baker | last post by:
Hello everybody, I'm new to C++ (I have some C background). I've read up on this topic a few times but it just dosen't seem to be sinking in. 1. Whats the difference between overloading and...
4
by: ORi | last post by:
Hi all ! There's a question I've been bothering for a while: I'm actually developing architectural frameworks for application developing and I think virtual methods, although needed because of...
2
by: ESPNSTI | last post by:
Hi, I'm very new to C# and .Net, I've been working with it for about a month. My experience has been mainly with Delphi 5 (not .Net). What I'm looking for is for a shortcut way to override a...
6
by: bryanbabula | last post by:
I have a question about overriding i was wondering if anyone could help me with, or even suggesting a better/different way. I have no idea if this can even be done or not. I was wondering if there...
8
by: yashwant pinge | last post by:
#include<iostream> using namespace std; class base { public: void display() { } };
12
by: danil52 | last post by:
Hello there, I have the following code: class Base { public: virtual void f() {cout << "Base::f()" << endl;} virtual void f(int) {cout << "Base::f(int)" << endl;} };
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.