Mar 25, 2013 at 8:03pm UTC
User Defined Types :
When a friend function returns an instance, what is the difference between the following operator functions?
1 2 3 4 5 6 7 8
//Operator+ 1 without constructor
MyClass operator +(const MyClass& ThisClass, const MyClass& ThatClass){
return ThisClass.number + ThatClass.number;
}
//Operator+ 2 using class constructor
MyClass operator +(const MyClass& ThisClass, const MyClass& ThatClass){
return MyClass(ThisClass.number + ThatClass.number);
}
Read-Only functions :
And... What is the difference between these two functions?
1 2 3 4 5 6
void MyClass::foo(const MyClass& ThisClass, const MyClass& ThatClass){
//Do something...
}
void MyClass::foo(MyClass& ThisClass, MyClass& ThatClass) const {
//Do same something
}
Last edited on Mar 27, 2013 at 5:17am UTC
Mar 25, 2013 at 8:15pm UTC
When you post some code then test it yourself that it at least is compiled.
This statement will not b compiled
friend MyClass operator+(const MyClass& ThisClass, const MyClass& ThatClass) const;
because functions that are not members of classes may not have the qualifier const
Last edited on Mar 25, 2013 at 8:16pm UTC
Mar 25, 2013 at 8:30pm UTC
Oop, sorry. That part you pointed out was a typo.
But when I tried compiling the operator+ functions, they both returned the same instance when given the same parameters. I was hoping to learn if there was any risk in using one method over another.
Mar 26, 2013 at 4:26am UTC
The const qalifier of non-static member functions means that access to class members is done using const this Thus these functions can not change objects that call these functions.
Last edited on Mar 26, 2013 at 8:03am UTC
Mar 26, 2013 at 9:10pm UTC
So, they are essentially the same?
Mar 27, 2013 at 5:40am UTC
I have not understood why did you make this resume. I pointed out already the difference.