0

I have created class ”A” with simple constructor that is printing a simple message on the screen:

A.hpp:

class A {
    public:
        A();
};

A.cpp:

#include "A.hpp"
#include <iostream>

A::A() {
    std::cout << "From the class A." << std::endl;
}

and the second class ”B” that inherits from ”A”:

B.hpp:

#include "A.hpp"
class B : public A{
    public:
        B();
};

B.cpp

#include "B.hpp"
#include <iostream>

B::B() {
    std::cout << "From the class B." << std::endl;
}

when i try to create instance of the class ”B”, i have the output:

From the class A.
From the class B.

So, how can i override the constructor to make it print just From the class B.?

  • 1
    It'a not possible. Exactly one constructor of each of the base classes will be called. – Yksisarvinen Jun 23 '21 at 14:14
  • 5
    This is not possible, C++ does not work this way. – Sam Varshavchik Jun 23 '21 at 14:15
  • Does this answer your question? [Why do we not have a virtual constructor in C++?](https://stackoverflow.com/questions/733360/why-do-we-not-have-a-virtual-constructor-in-c) – pptaszni Jun 23 '21 at 14:16
  • When you write `class B : public A` you are saying that `B` and `A` together form a single object. So both the `B` part and `A` part need to be constructed. The `A` part is constructed 1st then the `B` part, so you get a message from each of the constructors. – Richard Critten Jun 23 '21 at 14:16
  • 7
    is there some actual problem you are trying to solve? There is a way to get the desired output, just not by "overriding the constructor" – 463035818_is_not_an_ai Jun 23 '21 at 14:18
  • @463035818_is_not_a_number what way? im going to edit the title of the question. – Wzium Wzium Jun 23 '21 at 14:31
  • One way would be to have some kind of 'Init' method which is overridden in `B` and does not call the copy in the base class. – Paul Sanders Jun 23 '21 at 14:34
  • _"...How to call just one constructor in inherited class in C++?..."_ you can't - a class formed by inheritance is a compound object, each of the parts needs to be constructed. – Richard Critten Jun 23 '21 at 14:34
  • @PaulSanders it still calls the default constructor as the base does not exist until constructed (empty base class, pure interface classes excepted). – Richard Critten Jun 23 '21 at 14:35
  • @RichardCritten Sure, I'm assuming that the OP will move whatever code he wants excluded from the constructor to `Init`. – Paul Sanders Jun 23 '21 at 14:36
  • @PaulSanders would be easier just to remove the `std::cout` line from the base class. – Richard Critten Jun 23 '21 at 14:36
  • @RichardCritten I think that was just by way of example... – Paul Sanders Jun 23 '21 at 14:38
  • This is a [XY](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) problem. What is the root problem you are trying to solve? – bolov Jun 23 '21 at 15:05
  • I was more talking about the general possibility to get that output with completely different code. For example `int main() { std::cout << "From class A \nFrom class B"; }` Of course thats a silly answer, but what you actually need is unclear. Your question is only talking about what you consider as solution to something, but what is that something? – 463035818_is_not_an_ai Jun 23 '21 at 20:26

1 Answers1

0

You cannot get the desired output with your class design. In C++ all the base class constructors are called and there's no way to avoid this. I think a right answer to your question depends on which is your final porpouse.

Marco Beninca
  • 605
  • 4
  • 15