lundi 27 juillet 2015

Virtual copy constructor, assignment operator and polymorphism

I am trying to resolve this compile error regarding the assignment operator.

Suppose I have a base class:

class A
{
public:
virtual A change_to(const condition& cond){ }
protected:
 double i,j,k;
}

And child classes:

class B : public A
{
public:
A change_to(const condition& cond)
 {
 // do some operations depending on the condition
 return A;
 }
}

class C : public A
{
public:
A change_to(const condition& cond)
 {
 // do some operations depending on the condition
 return A;
 }
}

int main()
{
A elementA;
B elementB;
C elementC;
condition = enum::cond1 ; // assume enumeration is defined

Suppose I want to change element B into C based on a given condition. I would like to do something like this:

C=B.change_to(condition);  // I need a copy constructor and assignment operator

return 0;
}

How can I implement an assignment operator that takes into account the fact that the change is possible between any member of the children family of classes?

Aucun commentaire:

Enregistrer un commentaire