In class A, I do not define any constructor that takes parameters. In class B, which is a derived class of class A, I define a copy constructor and it passes a reference of B to the constructor of A. It is so weird to see this code does pass the compiling and the output is below. Could someone explain it a little bit?
// Example program
#include <iostream>
#include <string>
class A{
public:
A(){ std::cout<<"A constructor called"<<std::endl;};
~A(){};
};
class B:private A{
public:
B(){std::cout<<"B default constructor called"<<std::endl;};
B(const B& b): A(b){std::cout<<"B constructor called"<<std::endl;};
};
int main()
{
B b{};
B b2{b};
return 0;
}
output
A constructor called
B default constructor called
B constructor called