C++ - Instantiating derived class and using base class's constructor
Solution 1
You have two possibilities - inline:
class DerivedClass : public BaseClass {
public:
DerivedClass (string b) : BaseClass(b) {}
};
or out of line:
class DerivedClass : public BaseClass {
public:
DerivedClass (string b);
};
/* ... */
DerivedClass::DerivedClass(string b) : BaseClass(b)
{}
more examples:
class DerivedClass : public BaseClass {
public:
DerivedClass(int a, string b, string c);
private:
int x;
};
DerivedClass::DerivedClass(int a, string b, string c) : BaseClass(b + c), x(a)
{}
on initializer lists:
class MyType {
public:
MyType(int val) { myVal = val; } // needs int
private:
int myVal;
};
class DerivedClass : public BaseClass {
public:
DerivedClass(int a, string b) : BaseClass(b)
{ x = a; } // error, this tries to assign 'a' to default-constructed 'x'
// but MyType doesn't have default constructor
DerivedClass(int a, string b) : BaseClass(b), x(a)
{} // this is the way to do it
private:
MyType x;
};
Solution 2
If all you want to do is construct a derived class instance from a single parameter that you pass to the base class constructor, you can to this:
C++03 (I have added explicit, and pass by const reference):
class DerivedClass : public BaseClass {
public:
explicit DerivedClass (const std::string& b) : BaseClass(b) {}
};
C++11 (gets all the base class constructors):
class DerivedClass : public BaseClass {
public:
using BaseClass::BaseClass;
};
If you want to call different DerivedClass
constructors and call the BaseClass
constructor to some other value, you can do it too:
class DerivedClass : public BaseClass {
public:
explicit DerivedClass () : BaseClass("Hello, World!") {}
};
Comments
-
yeenow123 almost 2 years
I have a base class with a constructor that requires one parameter (string). Then I have a derived class which also has its' own constructor. I want to instantiate the derived class and be able to set the parameter of the base class's constructor as well.
class BaseClass { public: BaseClass (string a); }; class DerivedClass : public BaseClass { public: DerivedClass (string b); }; int main() { DerivedClass abc ("Hello"); }
I'm not sure how to set the base class constructor's parameter when calling the derived class.
-
yeenow123 over 11 yearsWhat if I want to set the base constructor's parameter value to something different than the derived class's parameter?
-
Wug over 11 years@yeenow123 then use
DerivedClass::DerivedClass(string b) : BaseClass("cat")
-
Fiktik over 11 yearsOf course you can. The context in the base class initializer is the same as inside the constructor body (except the members are not yet initialized, so you can't use them)
-
yeenow123 over 11 years@Wug I mean when creating an instance in the main() for example. I wouldn't want to hardcode the base class's parameter value.
-
Wug over 11 yearsCan you really think of no way to do it? Edit - I'll give you a hint. it involves commas.
-
MartyE over 11 years@yeenow123 What would you want to set the
Base
constructor parameter to? -
twalberg over 11 yearsThen you need
Derived::Derived(string a, string b) : Base(a) { something with b }
. -
yeenow123 over 11 yearsIn the int main() when creating the instance.. DerivedClass abc ("Hello") ("Goodbye"); "Hello" would be the DerivedClass's parameter and "Goodbye" the BaseClass's parameter. Is that clear?
-
Fiktik over 11 years@yeenow123 No, thats not possible. The way you pass values to your base class is entirely in the implementation of derived class - not in the hands of the derived class user
-
yeenow123 over 11 years@Fiktik Ah ok, that's what I wanted to know. Looks like I'll have to go about it another way. Thanks! Quick question, what is the x(a) doing in your last example?
-
Fiktik over 11 years@yeenow123 the
x(a)
thing is called initializer list - you can initialize your members the same way you pass values to the base class. In here it is the same as sayingx = a
in constructor body, becausex
is int. But ifx
was an object that needs an integer in its constructor, then the only way how to initialize such a member would be with the initializer list. I will add another example.