Dealing with User-Defined Types and Read-Only Functions

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
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
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.
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
So, they are essentially the same?
I have not understood why did you make this resume. I pointed out already the difference.
Topic archived. No new replies allowed.