Overriding base class

Discussion in 'C++' started by James Emil Avery, Jul 25, 2007.

  1. 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 <>
     
    James Emil Avery, Jul 25, 2007
    #1
    1. Advertising

  2. 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
     
    Michael DOUBEZ, Jul 25, 2007
    #2
    1. Advertising

  3. 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
     
    Michael DOUBEZ, Jul 25, 2007
    #3
  4. 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
     
    James Emil Avery, Jul 25, 2007
    #4
  5. James Emil Avery

    Guest

    On Jul 25, 11:32 pm, James Emil Avery <> wrote:
    > 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, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stuart McGraw
    Replies:
    10
    Views:
    479
    Scott David Daniels
    Feb 17, 2004
  2. alf

    overriding base class

    alf, Jun 30, 2007, in forum: Python
    Replies:
    2
    Views:
    301
    Michele Simionato
    Jun 30, 2007
  3. Guest
    Replies:
    2
    Views:
    454
  4. Floris Bruynooghe

    Overriding base class methods in the C API

    Floris Bruynooghe, Jan 19, 2009, in forum: Python
    Replies:
    1
    Views:
    287
    Gabriel Genellina
    Jan 19, 2009
  5. Tim S
    Replies:
    3
    Views:
    145
    Brian McCauley
    Jun 1, 2007
Loading...

Share This Page