0

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:

elementC=elementB.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?

the error is in the assignment operator=

Sam Gomari
  • 733
  • 4
  • 13
  • 37
  • 1
    You can't return a class, please provide real code. BTW: Do you know about "covariant" return types? Just guessing that could be related to what you are trying to do... – Ulrich Eckhardt Jul 27 '15 at 08:00
  • I don't really understand what you want, virtual constructors/assignment operators make no sense at all. You probably just want an overloaded constructor that takes an `A`? – AliciaBytes Jul 27 '15 at 08:02
  • The `change_to` function can't return an `A` object by value, because it will *always* be an `A` object (it can't be a `C` object). You need to make it a template function (`template T change_to(...) { ...; return T(); }`) – Some programmer dude Jul 27 '15 at 08:02
  • virtual copy constructor does not exist. – paper.plane Jul 27 '15 at 08:20
  • @JoachimPileborg But then it can't be overridden, because template member functions can't also be virtual. Sam, I suspect this is an instance of the [XY problem](http://mywiki.wooledge.org/XyProblem). Please back up and tell us what you're really trying to do. – cdhowie Jul 27 '15 at 08:29
  • what i am trying to do...is return a specific type (B or C or D objects which are child classes of A) depending on the condition, say condition cond is satisfied to certain level, then i want to return the result of computation and assigned it to elementC. – Sam Gomari Jul 27 '15 at 08:32
  • @cdhowie i think what i am trying to do is clear. the return types are all parent class A, maybe it shouldn't ...the idea is to use polymorphism to cast the child type in the return to assign to an expected type. – Sam Gomari Jul 27 '15 at 08:39
  • 1
    But you *can't* override properly. If you return an `A`object by value, then what is returned is a `A` object, the function can't ever return a `C` instance no matter what you do in the overridden function. If you want to return a sub-class of `A` then you either need to return by reference (which isn't really realistic) or dynamically allocate a new object of the proper type and return a pointer to `A` (which leads to other problems). For some related reading, read [about object slicing](http://stackoverflow.com/questions/274626/what-is-object-slicing). – Some programmer dude Jul 27 '15 at 08:49
  • 1
    I feel a code smell here. – vijairaj Jul 27 '15 at 10:43

0 Answers0