K
Kench
Sorry if this becomes a repost. I posted this to comp.lang.c++.moderated 1
hour ago still it does not show up there so posting this here.
Hi,
Consider class A & B both of which implement a copy constructor.
class B inherits from A.
When copy constructor of B is called, first the constructor of A gets called
My question is that, why the copy constructor for A not called.
Is there a way to have it call the copy constructor of class A?
#include <iostream>
using namespace std;
class A
{
public:
A() { cout << "A constructor called\n";};
A(A& a) { cout << "Copy A constructor called\n";};
~A() { cout << "Destructor A called\n";};
};
class B: public A
{
public:
B() { cout << "B constructor called\n";};
B(B& b) { cout << "Copy B constructor called\n"; };
~B() { cout << "Destructor B called\n";};
};
int main(void)
{
B b;
B bb = B(b);
return 0;
}
hour ago still it does not show up there so posting this here.
Hi,
Consider class A & B both of which implement a copy constructor.
class B inherits from A.
When copy constructor of B is called, first the constructor of A gets called
My question is that, why the copy constructor for A not called.
Is there a way to have it call the copy constructor of class A?
#include <iostream>
using namespace std;
class A
{
public:
A() { cout << "A constructor called\n";};
A(A& a) { cout << "Copy A constructor called\n";};
~A() { cout << "Destructor A called\n";};
};
class B: public A
{
public:
B() { cout << "B constructor called\n";};
B(B& b) { cout << "Copy B constructor called\n"; };
~B() { cout << "Destructor B called\n";};
};
int main(void)
{
B b;
B bb = B(b);
return 0;
}