Copy constructor witl pure virtual + pointer
Oct 22, 2011 at 6:22am UTC
Hi,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
// my A.h
class A // My interface
{
public :
A(){ std::cout << " A-Ctor " ;}
A(const A& other){std::cout << " A Copy Ctor " ;}
virtual ~A(){ std::cout << " A dtor " ; }
virtual void Foo() const =0;
};
// my B.h
class B : public A
{
public :
B(){ std::cout << " B-Ctor " ;}
virtual ~B(){ std::cout << " B-dtor " ; }
B(const B& other) :A(other){
std::cout << " B Copy Ctor " ;
}
virtual void Foo() const { }
};
//my main.cpp
// ... all includes
int main()
{
B b1;
A* = new A*(b1); // where copy made??
my Question in line
A* = new A*(b1);
where is the copy of object A* was made. what I know is that the copy constructors of A and B were not called !!
thanks.
Oct 22, 2011 at 7:30am UTC
I take it you mean A* a = new A(b1);
.
Well, you can't instantiate your abstract class A. You have to instantiate a derived class that fills in pure virtual functions.
Oct 24, 2011 at 6:44am UTC
kbw, thanks. now whick copy constructor was called?
Oct 24, 2011 at 7:07am UTC
The code does not compile. A is not instantiable because it has a pure virtual function (Foo()).
If we pretend Foo() is not there, A::A(const A &) would be called.
Topic archived. No new replies allowed.