Is the possible to have all the public constructors of the publicbase class as the constructors of a

Discussion in 'C++' started by Peng Yu, Sep 18, 2008.

  1. Peng Yu

    Peng Yu Guest

    Hi,

    I want B has all the constructors that A has. Obviously, the code
    below would not work. I could define a corresponding B's constructor
    for each A's constructor. But if A has many constructors, it would be
    inconvenient. I'm wondering if there is any way to inherent all A's
    constructor implicitly.

    Thanks,
    Peng

    class A{
    public:
    A(int x) : _x(x) { }
    private:
    int _x;
    };

    class B : public A { };
     
    Peng Yu, Sep 18, 2008
    #1
    1. Advertising

  2. Peng Yu

    Rolf Magnus Guest

    Re: Is the possible to have all the public constructors of the public base class as the constructors of a derived class?

    Peng Yu wrote:

    > Hi,
    >
    > I want B has all the constructors that A has. Obviously, the code
    > below would not work. I could define a corresponding B's constructor
    > for each A's constructor. But if A has many constructors, it would be
    > inconvenient. I'm wondering if there is any way to inherent all A's
    > constructor implicitly.


    There is no way. You have to define them yourself.
     
    Rolf Magnus, Sep 18, 2008
    #2
    1. Advertising

  3. Peng Yu

    Kai-Uwe Bux Guest

    Re: Is the possible to have all the public constructors of the public base class as the constructors of a derived class?

    Peng Yu wrote:

    > Hi,
    >
    > I want B has all the constructors that A has. Obviously, the code
    > below would not work. I could define a corresponding B's constructor
    > for each A's constructor. But if A has many constructors, it would be
    > inconvenient. I'm wondering if there is any way to inherent all A's
    > constructor implicitly.


    There is no way to inherit constructors. One reason is that a derived class
    may add more data members, which would need initialization.

    > class A{
    > public:
    > A(int x) : _x(x) { }
    > private:
    > int _x;
    > };
    >
    > class B : public A { };


    That said, you can forward constructors using templates. E.g., the following
    adds a virtual destructor:


    template < typename T >
    class virtual_destructor : public T {
    public:

    virtual_destructor ( void ) :
    T ()
    {}

    template < typename A >
    virtual_destructor ( A a ) :
    T ( a )
    {}

    template < typename A, typename B >
    virtual_destructor ( A a, B b ) :
    T ( a, b )
    {}

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

    template < typename A, typename B, typename C,
    typename D >
    virtual_destructor ( A a, B b, C c, D d ) :
    T ( a, b, c, d )
    {}

    template < typename A, typename B, typename C,
    typename D, typename E >
    virtual_destructor ( A a, B b, C c, D d, E e ) :
    T ( a, b, c, d, e )
    {}

    template < typename A, typename B, typename C,
    typename D, typename E, typename F >
    virtual_destructor ( A a, B b, C c, D d, E e, F f ) :
    T ( a, b, c, d, e, f )
    {}

    virtual ~virtual_destructor ( void ) {}

    }; // virtual_destructor<T>


    This requires some knowledge/guess about the maximum number of arguments in
    a constructor. Also, it does not handle reference parameters in
    constructors nicely.

    It is quite possible that this becomes simpler with variadic templates in
    the next standard.



    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Sep 18, 2008
    #3
  4. Kai-Uwe Bux wrote:
    > It is quite possible that this becomes simpler with variadic templates in
    > the next standard.


    I don't think it's just possible, but actually one of the main reasons
    why variadic templates are being added to the next standard: To allow
    perfect parameter forwarding.
     
    Juha Nieminen, Sep 18, 2008
    #4
  5. On Sep 19, 2:18 am, Peng Yu <> wrote:
    > Hi,
    >
    > I want B has all the constructors that A has. Obviously, the code
    > below would not work. I could define a corresponding B's constructor
    > for each A's constructor. But if A has many constructors, it would be
    > inconvenient. I'm wondering if there is any way to inherent all A's
    > constructor implicitly.
    >
    > Thanks,
    > Peng
    >
    > class A{
    > public:
    > A(int x) : _x(x) { }
    > private:
    > int _x;
    >
    > };
    >
    > class B : public A { };


    Hi Peng,

    I think constructors and destructors are not inheritable. Therefore,
    you can't override base class's constructors.

    cheers,
    Alex Kim
     
    Alexander Dong Back Kim, Sep 19, 2008
    #5
  6. Pete Becker wrote:
    > It also becomes unnecessary, because C++0x directly supports inheriting
    > constructors. <g>


    Well, sort of. You still have to explicitly write a
    "using BaseClass::BaseClass;" line in your derived class. The
    constructor inheritance is not automatic.

    Still better than having to explicitly write derived class
    constructors which call the equivalent base class constructors,
    though (because the "using" line will inherit *all* the constructors
    of the base class at once).
     
    Juha Nieminen, Sep 19, 2008
    #6
    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. Charles A. Lackman
    Replies:
    1
    Views:
    1,375
    smith
    Dec 8, 2004
  2. SpamProof
    Replies:
    0
    Views:
    583
    SpamProof
    Oct 21, 2003
  3. Kevin Spencer
    Replies:
    2
    Views:
    3,314
    Kevin Spencer
    Sep 15, 2004
  4. DaveLessnau
    Replies:
    3
    Views:
    429
    Howard
    May 16, 2005
  5. Chip Gore
    Replies:
    6
    Views:
    2,202
    Ben Voigt [C++ MVP]
    Apr 10, 2008
Loading...

Share This Page