overloading operators

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 =

This is in Derived class
1
2
  const	CreditCardPayment& CreditCardPayment::operator =(const CreditCardPayment& other)

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 =

I would call like this
 
Payment::operator =(other);


but if none like in my case how to call it?

my solution was using seters from Payment Class
1
2
3
this->setPaymentMethod(other.getMethod()); 
this->setOrderTotalAmount(other.getOrderTotalAmount());

Thx
Last edited on
closed account (o3hC5Di1)
Hi there,

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.

Hope that helps.

All the best,
NwN
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
Last edited on
closed account (o3hC5Di1)
Hi,

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.

All the best,
NwN

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.
closed account (o3hC5Di1)
@L B - would you please be so kind as to clarify? Perhaps with a little example? I'm just interested in understanding your point of view.

All the best,
NwN
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?
closed account (o3hC5Di1)
L B wrote:
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?

All the best,
NwN
You're still not getting my point.
1
2
3
4
Base *a = new Derived1;
Base *b = new Derived2;

*a = *b; //??? 
closed account (o3hC5Di1)
All right, I see what you mean, thanks for clarifying. So how would you solve the OP's question, which is basically:

1
2
3
4
5
6
7
8
9
10
11
12
struct base
{
    //members
};

struct derived
{
    derived& operator=(const derived& d)
    {
        //how to equal the base of "this" to the base of "d"?
    }
};


Thanks again,
NwN
I don't know, I would need to know why the OP thinks they need to use operator= for this.
Topic archived. No new replies allowed.