My objective is to do a deep copy of a class, but a virtual class is causing trouble.
#include<iostream>
using namespace std;
class Vir//pure virtual class
{
public:
virtual void hi()=0;
};
class Handler:public Vir
{
public:
int i;
Handler() {}
Handler(int val):i(val) {}
void hi() {cout<<"Value of i="<<i<<endl;}
int getI() const {return i;}
void setI(int j) {i=j;}
};
class ControlPanel
{
public:
Vir *v;
ControlPanel(const ControlPanel& c)//copy constructor
{
v=new Handler;
v->setI(c.getI());
}
int getI()const {return v->getI();}
void initialize() {v=new Handler(10);}
void hi() {v->hi();}
ControlPanel() {}
~ControlPanel() {delete v;}
};
int main()
{
ControlPanel cc;
cc.initialize();
cc.hi();
ControlPanel bb(cc);//copying cc into bb
}
The compilation error message:
test.cpp: In copy constructor ‘ControlPanel::ControlPanel(const ControlPanel&)’:
test.cpp:28: error: ‘class Vir’ has no member named ‘setI’
test.cpp: In member function ‘int ControlPanel::getI() const’:
test.cpp:30: error: ‘class Vir’ has no member named ‘getI’
I plan to have plenty more Handler classes (like Handler1, Handler2 etc) which inherit from Vir and will have their own unique members (like float a; or double b; etc). So it doesn't make sense for me to keep all the getter & setter functions of all Handler classes in the Vir class. I want to keep my getter and setter methods in the Handler classes because the members are unique to the Handler classes. The compiler is not allowing me to do so. Help?
Add a
duplicate()
function to your abstract class, which (in each derived class) creates a new instance with the right values and returns it. Alternatively, consider acopyFrom(Abs other)
function which checks to ensure that you're copying from the correct type and if so, copies the fields out.In general, if your ControlPanel class has a reference to an Abs object, it shouldn't be trying to do its duplication by inspecting the concrete Handler object, it should be passing the duplication off to a virtual function on that object.
Maybe I am missing something but would you not be better with a virtual
clone
method onVir
? This means you can avoid the nasty cast in theControlPanel
copy constructor outlined in your own answer. This is the same as @Andrew Aylett suggests in his answer withduplicate
being used instead ofclone
.Something like
which is implemented in
Handler
to beNote the use of the covariant return type i.e.
Handler::clone
is allowed to return aHandler*
rather than just aVir*
and still be a valid override ofVir::clone
.This makes the
ControlPanel
copy constructor simplyYou have to define
getI
andsetI
as (pure) virtual inVir
to make them accessible via subclasses. No way around this.Change
Vir *v
toHandler *v;
and see whether your code compiles or not.Your class
Vir
doesn't declare/definesetI()
andgetI()
member functions.Or define
Vir
asAs Steve suggested, I'm answering my own question coz a friend gave me a solution. Hope this would be of help to anyone who has the question of how to do a Deep copy in C++ where a virtual class may be a roadblock. Hope someone finds this useful.
Why would the compiler allow you? Those methods are not on that interface.
You could use the Factory Pattern to create your
Vir
, to avoid having to add all of the constructors toVir
's interface. You should also consider using RAII to avoid initialize() style functions.