From what I've understood a Copy Constructer is called when an object is passed by value in a method.
So if I implement my own Copy Constructer, is there a way to avoid slicing when I pass an object by value in a method?
Example :
// in headers file
// for A
class A
{
public :
virtual void Display() const
{ cout << "A::Display() was called << endl; }
};
// for B
class B : public A
{
public :
void Display() const
{ cout << "B::Display() was called << endl; }
//---> here I would created a copy constructer
};
in a main.cpp file I do this :
void f( A anA)
{
anA.Display();
}
int main()
{
B anB;
f ( anB );
return 0;
}
From what I understand, when this is done, anB is copied into anA ( the formal parameter of the method f) :
f ( anB );
Is there a way for it to output "B::Display() was called" (without passing by reference or using a pointer)?
Thanks!