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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peng Yu

    #1

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

    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 { };

  • Rolf Magnus

    #2
    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.

    Comment

    • Kai-Uwe Bux

      #3
      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_destruc tor : public T {
      public:

      virtual_destruc tor ( void ) :
      T ()
      {}

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

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

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

      template < typename A, typename B, typename C,
      typename D >
      virtual_destruc tor ( 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_destruc tor ( 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_destruc tor ( A a, B b, C c, D d, E e, F f ) :
      T ( a, b, c, d, e, f )
      {}

      virtual ~virtual_destru ctor ( void ) {}

      }; // virtual_destruc tor<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

      Comment

      • Juha Nieminen

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

        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.

        Comment

        • Pete Becker

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

          On 2008-09-18 09:31:03 -0700, Kai-Uwe Bux <jkherciueh@gmx .netsaid:
          >
          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.
          >
          It also becomes unnecessary, because C++0x directly supports inheriting
          constructors. <g>

          --
          Pete
          Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
          Standard C++ Library Extensions: a Tutorial and Reference
          (www.petebecker.com/tr1book)

          Comment

          • Juha Nieminen

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

            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::Base Class;" 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).

            Comment

            • Pete Becker

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

              On 2008-09-19 03:19:27 -0700, Juha Nieminen <nospam@thanks. invalidsaid:
              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::Base Class;" line in your derived class. The
              constructor inheritance is not automatic.
              Of course not. Automatic inheritance of constructors would be a
              disaster. Nevertheless, inherited constructors will be supported in
              C++0x.
              >
              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).
              Indeed; that's how inheritance works.

              --
              Pete
              Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
              Standard C++ Library Extensions: a Tutorial and Reference
              (www.petebecker.com/tr1book)

              Comment

              Working...