How do I get the constructor to be inherited by derived class?

This is a discussion on How do I get the constructor to be inherited by derived class? within the C++ Programming forums, part of the General Programming Boards category; If you overload a class' constructor and then use that new constructor, are derived classes then forced to define the ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    40

    How do I get the overloaded constructor to be recognised by derived class?

    If you overload a class' constructor and then use that new constructor, are derived classes then forced to define the new constructor as well?

    With the default constructor, I could just do something like this, obviously.
    Code:
    class object1
    {
      public:
      object1(void)
      {
        cout << "A new object has been created.\n";
      }
    };
    class object2: public object1
    {
    };
    Then, when you have a new object2, obviously it still outputs "A new object has been created." and you don't have to do anything about it if you want that.

    However, if I do object1(int I) instead of object1(void), it does not seem to work the same way. My new object definitions are getting annoying to work with as I have to keep redefining the same constructors over and over the way I have it set up, and that will become a big problem if I want to create a lot of class types (such as a lot of types of item with my item class).

    This is what I have currently.
    Code:
    class atom
    {
      public:
      atom(){initialize();}
      atom(int I){}
      atom(atom *A)
      {
        A->entered(this);
      }
    };
    class item: public atom
    {
      public:
      item(void) : atom(0)
        { initialize();}
      item(atom *A) : atom(A)
        { initialize();}
    };
    class test_item: public item
    {
      public:
      test_item(void)
        { initialize();}
      test_item(atom *A): item(A)
        {initialize();}
    };
    If I take away the redefinitions of the constructor for test_item and try to create test_item by passing an atom pointer to it, such as the following
    Code:
    int main(int argc, char *argv[])
    {
      test_item I(locate(0,0,0));
      //locate is a function that successfully returns another atom
      //...
    If I do that without having redefined the constructor overload, I get an error stating that there is no matching function call for using the constructor that way.

    It works fine if I continually redefine the dang things all the time, but I was thinking that there must be a way to get them inherited so that I don't have to.
    Last edited by Loduwijk; 03-25-2006 at 04:09 PM. Reason: Making title more descriptive

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,266
    If you want test_item to have a constructor that takes arguments, test_item needs to provide that constructor ...... even if all it does is forward the argument to the base class constructor.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    40
    Quote Originally Posted by grumpy
    If you want test_item to have a constructor that takes arguments, test_item needs to provide that constructor ...... even if all it does is forward the argument to the base class constructor.
    *sigh* That's what I was afraid of. Thanks.

  4. #4
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    simply put: constructors are never inherited.
    Would be rather silly if you think of it. They have the wrong name after all...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 11:36 AM
  2. derived class can not access base class protected member?
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2007, 06:32 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. derived class member function pointer
    By btq in forum C++ Programming
    Replies: 2
    Last Post: 10-20-2002, 12:41 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21