+ 3

if the base class has a overloaded constructor, can we specify which to be called when creating a derived class from the bass class?

30th Apr 2016, 3:19 PM
Moria
1 Odpowiedź
+ 6
Base class constructors are automatically called for you if they have no argument. If you want to call a superclass constructor with an argument, you must use the subclass's constructor initialization list. For example: class A { public: A(int foo) { // do something with foo } }; class B: public A { public: B(int foo, int bar) : A(foo) // Call the superclass constructor in the subclass' initialization list. { // do something with bar } };
26th May 2016, 1:24 PM
James Flanders