Original Post
Hi, Let's say I have this code: So the main class contains a pointer to B, which can take the form of either D1 or D2. However, how do you then write the copy ctor and copy assignment code? How do you know what 'new' to call for b_? Is it just a memcpy call? Thanks in advance for any help, Jim.
// B is purely an abstract interface
class B { /* details */ };
class D1 : public B { /* details */ };
class D2 : public B { /* details */ };
class Main
{
private:
B* b_;
public:
// Ctor and dtor
Main() : b_(new D1) {}
dMain() {delete b_;} // Destructor - alt 126 doesn't work here!
// Change data
void ChangeToD1() {delete b_; b_ = new D1;}
void ChangeToD2() {delete b_; b_ = new D2;}
// Copy assignment and constructor
Main(const Main& rhs);
Main& operator=(const Main& rhs);
};