Hello
I have simple question
i have base class (Payment) and a derived class ( CreditCardPayment) .
In derived class there is an implantation of overloading operator =
Now from what i understand after i finish updating all variables
in CreditCardPayment i need to update them in Payment class.
If in Payment class was also implantation of overloading operator =
If you need operator=() overloaded in a child class, it's probably best to do it for the Base class too (unless it is an interface). This is especially true if the Base class contains any pointer members.
Otherwise, using setters will work, or you could make the members of Payment "protected", which will allow Payment's derived classes to access them directly, although that doesn't always make sense design-wise. For instance, your CreditCardPayment constructor should probably set its "paymentmethod" to "creditcard", since a creditcardpayment is never going to be used for anything else.
You don't necessarily have to use this-> within the class by the way, since CreditcardPayment is derived from Payment, it inherits the functions and you can call them just by their identifier.
Hi
I thought maybe some thing like this
[code]
const CreditCardPayment& CreditCardPayment::operator =(const CreditCardPayment& other)
{
static_cast<&Payment>*CreditCardPayment=static_cast<&Base>*( other)
}
[\code]
I know that i don't need to implement in base class overloadin on operator= unless i have some pointers in it,and in my case i have only enum and int
Thx
Using the setters will be a lot clearer to anyone reading the code. So will overloading operator=() in the base class. Why are you reluctant to do so, it seems like the easiest and cleanest fix of all? Now you will have to copy all these setters to whichever derived class you make, whilst if you overload =() in the base class, you can just call that every time.
For your example - you are using operator=() on CreditCardPayment with its base class as right argument, I think you would have to overload CreditCardPayment's operator=() specifically for that.
Operator overloading does not go hand-in-hand with polymorphism, especially the assignment operators. Why do you need operator overloading if you're using polymorphism? I've not yet heard of a way to combine the two that works in any expected way.
Can you give me an example of when you think it makes sense?
So we have a Base class, and Derived1 and Derived2 classes. For one, each class has to have it's own assignment operator, and then the derived classes have to override the base class assignment operator.
But then what does it mean to assign an instance of Derived1 to an instance of Derived2? Why would you even need or want to?
But then what does it mean to assign an instance of Derived1 to an instance of Derived2? Why would you even need or want to?
Ah, no - that's not what I meant. My point was simply that for the OP it would be the easiest solution to provide an operator=() in the Base class and to call Base::operator=(Derived) in the derived classes' operator=().
Your former post gave me the impression that you were saying that when you're using polymorphism, operator overloading does not make sense in general, which confused me a little bit.
Please tell me if I'm still not getting your point?