is overloading operators syntext just a matter of experience

i know there is little chance i will need to use overloaded operators but im trying to remember how just in case i do. the problem is outside of remembering that with operator= i need to check for self assignment im having trouble remembering the params of all the operators. is this just a matter of experience. like some require a constant reference like the == operator (i think) while others dont use constant while still others dont use any reference at all. i just dont know if i should move on with my learning and just have to look it up when i need them or if i should study them more so i know and write them down. i really wanna know this stuff.
look it up when i need them
im having trouble remembering the params of all the operators.


There isn't really anything special you have to remember. They're just like any other function.

like some require a constant reference


Passing by value or by const reference is something you should be familiar with for all C++ coding. It is nothing that is specific to operator overloading. It applies to any function all the same.

Passing by value creates a copy.
Passing by const reference does not.
i know the difference between pass by reference and pass by value. its just that i keep forgetting which one it calls for. im guessing i just have to write it more my memory isnt that great.
i know the difference between pass by reference and pass by value. its just that i keep forgetting which one it calls for


For complex objects where copying might be expensive, pass by const reference.

For basic types like int, double, etc where copying is cheap, pass by value.

what they said...

so if you create a function this way (using an operator since that's what you asked):

1
2
3
4
bool operator==(myClass var1, myClass var2) 
{
  //...
}


you are creating a copy of the vars, so it will cost you. This way:

1
2
3
4
bool operator==(const myClass &var1, const myClass &var2) 
{
  //...
}


Sends a reference, but makes it const to prevent changing (and so you can send constants). So there is no cost of copying...
Last edited on
You can read http://www.cplusplus.com/articles/jsmith1/ for more on copy constructors
and assignment operators and http://www.cplusplus.com/forum/articles/20193/ to learn
how to tell when to use pointers, references etc. Also, if you enter "C++ operators" into
Wikipedia, you'll get a page that shows the declarations for all operators.
that is extremely helpful jsmith. i will look into all of that. thanks for posting.
Here is another reference that includes some code examples that you may find useful.

http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fcplr323.htm
Topic archived. No new replies allowed.