Sign in to follow this  

Overloading Operators Question...What about normal operator implementation

This topic is 4200 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In learning C++ I have come across Overloading operators. At first I was thinking that it was strictly because you "can" manipulate the meaning for an operator so when it is used with a class you are actually calling a member function to perform some operation. However, I have two question since learning about overloading operators: 1) what happens when you just use the operatore in your program expecting it to behave as the default representation? What happens when you want to use that operatore that you have overloaded with class objects and want to use it in as the default representation and not be calling a member function? 2) Is there any "must use" circumstances that you have to define a overload operator for a class or is it strictly for objective reasons? Thanks in advance,

Share this post


Link to post
Share on other sites
Quote:
1) what happens when you just use the operatore in your program expecting it to behave as the default representation? What happens when you want to use that operatore that you have overloaded with class objects and want to use it in as the default representation and not be calling a member function?


Well most operators you overload don't have a default definition with classes, so the overloaded version is the only version. In the cases where you do have a default definition (e.g. =) I don't think you can use it if you've overloaded it.

Quote:
2) Is there any "must use" circumstances that you have to define a overload operator for a class or is it strictly for objective reasons?


In the STL there's various constraints on the types you use with various containers and functions, and in some cases you do have to have an overloaded operator defined. E.g. the std::sort function requires that the elements you're sorting have a < operator defined (otherwise it doesn't know what order to sort them in).

Share this post


Link to post
Share on other sites
You misunderstand operator overloading. The goal of operator overloading is to define the operator's behavior (ie the way it operates), not to change its meaning. = is not +, and + is not /.

Moreover, as soon as you quit the realm of artithmetic types, most arithmetic operators become undefined. You can't add two structures? You can't multiply them. If you want to do so, you have two possibilities: writing a function of overloading the corresponding operator.

Eitherway, it is your choice to overload the operator - it is never a requirement, unless you use a library that needs it. Most of the time, a function or a member function is just as good as any operator overload.

HTH,

Share this post


Link to post
Share on other sites
Quote:
Original post by Monder
Quote:
2) Is there any "must use" circumstances that you have to define a overload operator for a class or is it strictly for objective reasons?


In the STL there's various constraints on the types you use with various containers and functions, and in some cases you do have to have an overloaded operator defined. E.g. the std::sort function requires that the elements you're sorting have a < operator defined (otherwise it doesn't know what order to sort them in).


Not *required*; you can also tell it by providing a comparison function. However, if you want to use an object for "the comparison function" (note that this is a *good* idea generally), that object in turn needs to have an overloaded operator defined: the 'operator()' (which allows you to "call" the object as if it were a function). This is particular useful when there are several, unrelated, meaningful ways to sort the objects. (If they're related, you can sometimes take shortcuts: e.g. if you want descending order instead of ascending, you can use 'std::greater<your object type>' for the comparison function.)

Share this post


Link to post
Share on other sites

This topic is 4200 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this