How to Call the Base Class Constructor

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.

Calling the base constructor in C#

Modify your constructor to the following so that it calls the base class constructor properly:

public class MyExceptionClass : Exception
{
public MyExceptionClass(string message, string extrainfo) : base(message)
{
//other stuff here
}
}

Note that a constructor is not something that you can call anytime within a method. That's the reason you're getting errors in your call in the constructor body.

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.

Calling a constructor of the base class from a subclass' constructor body

The following is an excerpt from "Accelerated C++":
"Derived objects are constructed by:

1. Allocating space for the entire object (base class members as well as derived class members);

2. Calling the base-class constructor to initialize the base-class part of the object;

3. Initializing the members of the derived class as directed by the constructor initializer;

4. Executing the body of the derived-class constructor, if any."

Summarizing the answers and comments: Calling a constructor of the base class from a subclass' constructor body is impossible in the sense that #2 above must precede #4.
But we still can create a base object in the derived constructor body thus calling a base constructor. It will be an object different from the object being constructed with the currently executed derived constructor.

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 initialize
sizeF, 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.)

C++ Inheritance: Calling Base Class Constructor In Header

In Child.h, you would simply declare:

Child(int Param, int ParamTwo);

In Child.cpp, you would then have:

Child::Child(int Param, int ParamTwo) : Parent(Param) {
//rest of constructor here
}

C# - Call base and class constructors in derived class

Not to say this is the CORRECT answer (and I'd love to hear how others would recommend I do this), but what I ended up doing was creating a private setter method that either constructor can call along the following lines:

class DerivedClass : BaseClass
{
public DerivedClass(string somethingNew)
: base()
{
setVals(somethingNew);
}
public DerivedClass(string somethingNew, int someVal)
: base(someVal)
{
setVals(somethingNew);
}

private setVals(string somethingNew)
{
// do something with the somethingNew varible
}
}

It saved my issue of having to deal with repetitive code and seems to be the cleanest way to do this, but, as I said, I'd love to see what others recommend / if there's a way to do this better.

Thanks!!!

Will the base class constructor be automatically called?

This is simply how C# is going to work. The constructors for each type in the type hierarchy will be called in the order of Most Base -> Most Derived.

So in your particular instance, it calls Person(), and then Customer() in the constructor orders. The reason why you need to sometimes use the base constructor is when the constructors below the current type need additional parameters. For example:

public class Base
{
public int SomeNumber { get; set; }

public Base(int someNumber)
{
SomeNumber = someNumber;
}
}

public class AlwaysThreeDerived : Base
{
public AlwaysThreeDerived()
: base(3)
{
}
}

In order to construct an AlwaysThreeDerived object, it has a parameterless constructor. However, the Base type does not. So in order to create a parametersless constructor, you need to provide an argument to the base constuctor, which you can do with the base implementation.

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.



Related Topics



Leave a reply



Submit