-1

If my base class's constructor takes parameters, how do I create a class that inherits from it, and executes the base class's constructor using the derived class's constructor?

For example, assuming my base class's instructor takes an integer, and does a cool trick with it, how would I make it so all classes that inherit from it can do the same

Pseudo-code

baseClass instance(99); //does an awesome trick, nice
derivedClass instance(99); //<-- should do the same as above

As I have it now, I have my base class's constructor defined, but for the derived class, I left its constructor blank, since I want it to do exactly what the base constructor does, and nothing more. But, this gave me an error along the lines of: no matching function call to the 'baseClass()', candidates are: baseClass(int), candidate expects 1 argument, 0 provided

Do I have to make the base class's constructor virtual or something?

gabriel.hayes
  • 2,267
  • 12
  • 15
  • edit: that was bad information. Update: you use `derivedClass() : baseClass(7){}` – Dave Dec 01 '13 at 14:19
  • 1
    Use a [member initialization list](http://stackoverflow.com/questions/1711990/what-is-this-weird-colon-member-syntax-in-the-constructor/8523361#8523361). – Alok Save Dec 01 '13 at 14:20
  • Also, unless `base` contains a pure virtual function, you should remove the `abstract-class` tag. –  Dec 01 '13 at 14:20
  • Well what if the base class's constructor alters the instance of the base class that uses it? Will it also alter the instance of the derived class? @Dave – gabriel.hayes Dec 01 '13 at 14:21
  • @user1538301, alters how? There is no valid instance until after the constructor is run. – StoryTeller - Unslander Monica Dec 01 '13 at 14:21
  • @remyabel yeah, I originally had a question about abstraction that I removed. I was going to ask if I made the base class abstract, and just moved the constructor's implementation into the derived classes would that work. Well, I figured it would, but it wouldn't be the best way to do things I think, since I'd be copypasting the same code for every single derived class – gabriel.hayes Dec 01 '13 at 14:22
  • @StoryTeller Variable initializations. For instance if you pass x and y into the base class, it by default sets the base class's x and y coordinates to those passed in. – gabriel.hayes Dec 01 '13 at 14:24

2 Answers2

2

This is easy in the current C++ standard, but it still requires some action on your part:

struct baseClass
{
  explicit baseClass(int);
};

struct derivedClass : baseClass
{
  using baseClass::baseClass;  // inherit *all* of baseClass' constructors
};

In the previous standard, C++03, you have to implement the constructor in the derived type and call the base class's one in the initialization list:

struct derivedClass : baseClass
{
  explicit derivedClass(int i) : baseClass(i) {}
};

Note: I use struct here to save typing: default member access and inheritance is public.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1

The idea is that derivedClass will need to call baseClass constructor anyway, since it needs to to complete its construction. The error you get is due to the fact that, without calling a the base constructor explicitly, the compiler is trying to call the default one.

However, in your case, it does not exist, since you already defined a constructor that takes an int. To do what you want, a solution could be to do this:

class derivedClass : baseClass {
    public:
        derivedClass(int x) : baseClass(x) {}
};
Svalorzen
  • 5,353
  • 3
  • 30
  • 54