Calling the base class constructor from the derived class constructor
The constructor of PetStore
will call a constructor of Farm
; there's
no way you can prevent it. If you do nothing (as you've done), it will
call the default constructor (Farm()
); if you need to pass arguments,
you'll have to specify the base class in the initializer list:
PetStore::PetStore()
: Farm( neededArgument )
, idF( 0 )
{
}
(Similarly, the constructor of PetStore
will initializesizeF
, by calling the constructor of Farm
. The constructor of a class always calls the constructors of
all of its base classes and all of its members.)
Is it possible to call a derived class constructor from a base class constructor?
Short answer: no. That would be breaking the object-oriented paradigm, so you wouldn't want to be able to do that anyway. Think about it this way: an abstract base class can be extended by an arbitrary number of classes, but if it was tightly coupled to one of the child classes, how would that impact the other ones?
public class BaseClass {
// Call child class constructor
public BaseClass() : A() { }
}
public class A : BaseClass {
public A() { ... }
}
// How should BaseClass handle this? There is no constructor named "A."
public class B : BaseClass {
public B() { ... }
}
If you want the base class and derived class to share some functionality, you should make it a protected method in the base class. That way, you can call that method from the constructor.
You could also make a constructor on the base class that provides the common functionality and call it from the child class with : base(...)
.
Can I call a derived class constructor from a base class?
You cannot call a derived class constructor from a base class. The base class has no idea the derived class even exist.
How to get around calling base class constructor from derived class with the same arguments?
You may use a using-declaration to
Using-declaration
[...] introduce base class members into derived class definitions.
[...]
Inheriting constructors
If the using-declaration refers to a constructor of a direct base of the class being defined (e.g. using
Base::Base;
), all constructors of that base (ignoring member access) are made visible to overload resolution when initializing the derived class.If overload resolution selects an inherited constructor, it is accessible if it would be accessible when used to construct an object of the corresponding base class: the accessibility of the using-declaration that introduced it is ignored.
as such:
class B : public A {
using A::A;
};
Note the highlight that the accessibility of the using-declaration itself is ignored, meaning using A::A
in the example needn't the be placed under public
accessibility - what matters is the accessibility of the inherited constructors in B
.
What are the rules for calling the base class constructor?
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. Unlike Java, C++ supports multiple inheritance (for better or worse), so the base class must be referred to by name, rather than "super()".
class SuperClass
{
public:
SuperClass(int foo)
{
// do something with foo
}
};
class SubClass : public SuperClass
{
public:
SubClass(int foo, int bar)
: SuperClass(foo) // Call the superclass constructor in the subclass' initialization list.
{
// do something with bar
}
};
More info on the constructor's initialization list here and here.
derived class constructor call base constructor
Since size
is a private
variable in the base class Array
, it cannot be accessed in the child class NumericArray
.
There are two ways to access size
in the child class NumericArray
:
Make
size
a protected variable in the base classArray
and access it in the child classNumericArray
.protected: int size;
Write a
getter
public function (GetSize()
) in the base classArray
that returnssize
, and the child classNumericArray
can just call thisgetter
public function in the constructor of the child class usingsuper.GetSize()
per this link.public:
GetSize() { return size }
C++ calling base class constructors
The short answer for this is, "because that's what the C++ standard specifies".
Note that you can always specify a constructor that's different from the default, like so:
class Shape {
Shape() {...} //default constructor
Shape(int h, int w) {....} //some custom constructor
};
class Rectangle : public Shape {
Rectangle(int h, int w) : Shape(h, w) {...} //you can specify which base class constructor to call
}
The default constructor of the base class is called only if you don't specify which one to call.
How do I call the base class constructor?
You do this in the initializer-list of the constructor of the subclass.
class Foo : public BaseClass {
public:
Foo() : BaseClass("asdf") {}
};
Base-class constructors that take arguments have to be called there before any members are initialized.
Related Topics
What Does _Declspec(Dllimport) Really Mean
Why Is Std::Fill(0) Slower Than Std::Fill(1)
How to Generate Thread-Safe Uniform Random Numbers
The Intersection of Two Sorted Arrays
Std::Unique_Ptr for C Functions That Need Free
Universal Less<> for Pointers in C++ Standard
C++ Class Member Function Pointer to Function Pointer
Iterate Through a C++ Vector Using a 'For' Loop
Differencebetween a Concrete Class and an Abstract Class
Cleaning Up an Stl List/Vector of Pointers
Performance Cost of Passing by Value VS. by Reference or by Pointer