problem on inheritance

Discussion in 'C++' started by shuisheng, Sep 27, 2006.

  1. shuisheng

    shuisheng Guest

    Dear All,

    I have a question on inheritance. Assume I have a base class Vector and
    its derived class DecoratedVector as follows:

    class Vector
    {
    // Constructors.
    Vector();
    Vector(size_t num, double initValue);
    Vector(const double *pData, size_t num);
    ... // Other several constructors.
    };

    class DecoratedVector : public Vector
    {
    ...

    private:
    size_t id;
    };

    The DecoratedVector is in fact a Vector with a decorated id. What I am
    wondering is that if there is any way to ease the work of writing the
    constructors for class DecoratedVector, since they are always the same
    as those in class Vectors.

    Thank you for your kind help!

    Shuisheng

    --
    N0 comp.lang.c++, NO my c++ life.
     
    shuisheng, Sep 27, 2006
    #1
    1. Advertising

  2. shuisheng wrote:
    > I have a question on inheritance. Assume I have a base class Vector
    > and its derived class DecoratedVector as follows:
    >
    > class Vector
    > {
    > // Constructors.
    > Vector();
    > Vector(size_t num, double initValue);
    > Vector(const double *pData, size_t num);
    > ... // Other several constructors.
    > };
    >
    > class DecoratedVector : public Vector
    > {
    > ...
    >
    > private:
    > size_t id;
    > };
    >
    > The DecoratedVector is in fact a Vector with a decorated id. What I am
    > wondering is that if there is any way to ease the work of writing the
    > constructors for class DecoratedVector, since they are always the
    > same as those in class Vectors.


    No, there isn't. Constructors are not inherited, so you have to write
    all constructor that will have the empty bodies and follow this pattern:

    DecoratedVector(type arg) : Vector(arg) {}

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Sep 27, 2006
    #2
    1. Advertising

  3. shuisheng

    Kai-Uwe Bux Guest

    shuisheng wrote:

    > Dear All,
    >
    > I have a question on inheritance. Assume I have a base class Vector and
    > its derived class DecoratedVector as follows:
    >
    > class Vector
    > {
    > // Constructors.
    > Vector();
    > Vector(size_t num, double initValue);
    > Vector(const double *pData, size_t num);
    > ... // Other several constructors.
    > };
    >
    > class DecoratedVector : public Vector
    > {
    > ...
    >
    > private:
    > size_t id;
    > };
    >
    > The DecoratedVector is in fact a Vector with a decorated id. What I am
    > wondering is that if there is any way to ease the work of writing the
    > constructors for class DecoratedVector, since they are always the same
    > as those in class Vectors.


    To some degree, you can use templated constructors:

    class DecoratedVector : public Vector {
    public:

    DecoratedVector ( void ) :
    Vector ()
    {}

    DecoratedVector ( DecoratedVector const & other ) :
    Vector ( other )
    {}

    template < typename A >
    DecoratedVector ( A a ) :
    Vector ( a )
    {}

    template < typename A, typename B >
    DecoratedVector ( A a, B b ) :
    Vector ( a, b )
    {}

    template < typename A, typename B, typename C >
    DecoratedVector ( A a, B b, C c ) :
    Vector ( a, b, c )
    {}

    virtual ~DecoratedVector ( void ) {}

    }; // DecoratedVector

    This will forward all constructors with up to 3 arguments.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Sep 27, 2006
    #3
    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. maxw_cc
    Replies:
    1
    Views:
    3,160
    Martijn van Steenbergen
    Dec 21, 2003
  2. cppsks
    Replies:
    0
    Views:
    833
    cppsks
    Oct 27, 2004
  3. karthikbalaguru
    Replies:
    9
    Views:
    1,055
  4. Daniel Pitts
    Replies:
    27
    Views:
    1,927
    Mike Schilling
    Feb 27, 2008
  5. johnsonlau
    Replies:
    1
    Views:
    783
    Kai-Uwe Bux
    Jul 21, 2008
Loading...

Share This Page