×

Loading...

Another way, only need one member function to copy itself.

#include "stdafx.h"
#include <iostream.h>

class Base
{
public:
int getTT()
{
return tt;
}
virtual void did(){};
void done();

Base *pBase;

protected:
void membercopy(Base *p)
{
tt = p->tt;
// and so on...
}
private:
int tt;
};

class Derived : public Base
{
public:
Derived(Base *p)
{
membercopy(p);
}
void did()
{
cout << "this is derived class" << endl;
cout << "tt = " << getTT() << endl;
}
private:
};

void Base::done()
{
tt = 8;
Base* b = new Derived(this);
b->did();
delete b;
}


int main(int argc, char* argv[])
{
Base b;
b.done();
return 0;
}
Report

Replies, comments and Discussions: