C++ operator overloading and private members

In summary, the conversation discusses a piece of code that adds two numbers together using a class and operator overloading. The code compiles and gives an output of 11, but the person is confused about how the operator function can access a private member of a different instance of the same class. The expert explains that since the operator function is a member of the class, it has access to all private members of any instance of the class.
  • #1
sir_manning
66
0
Good evening

I'm in the midst of reviewing for my exam, I've come across a piece of code I don't understand. It is as follows:

//////////////////////////
#include <iostream>
using namespace std;

class number{

private:
int value;

public:
number(int v){value = v;}

void operator + (const number & num){
value = value + num.value; //<-what is going on here?
}

void print(){cout<<"\n"<<value<<"\n";}

};

int main(void){
number five(5);
number six(6);

five+six;
five.print();

return 0;
}

////////////////////////

This code compiles and gives an output of 11. However, it seems to me that there is a privacy violation on the line indicated. How is it that the operator function can access a private member ("value") of another number class ("num")?

Thanks.
 
Technology news on Phys.org
  • #2
sir_manning said:
How is it that the operator function can access a private member ("value") of another number class ("num")?
That's not accurate: it is accessing a private member ("value") of a different instance ("num") of the same class ("number"). Since operator+ is a member of number, it has access to all of the private members of any instance of the number class.
 
  • #3
Aha... Thanks
 

What is operator overloading in C++?

Operator overloading in C++ is the process of defining new meanings for existing operators in a class. This allows for customized behavior of operators when used on objects of that class.

Why is operator overloading useful?

Operator overloading can make code more intuitive and readable by allowing objects to be manipulated using familiar operators. It also allows for more efficient and concise code by reducing the number of function calls needed.

Can private members be accessed in operator overloading?

No, private members cannot be directly accessed in operator overloading because they are only accessible within the class they are defined in. However, you can use public member functions to indirectly access and modify private members.

What is the difference between overloading unary and binary operators?

Unary operators operate on a single operand, while binary operators operate on two operands. This means that unary operators only require one parameter in their overloaded function, while binary operators require two.

Can any operator be overloaded in C++?

Not all operators can be overloaded in C++. Some operators, such as the member access operator (.), the scope resolution operator (::), and the ternary operator (?:), cannot be overloaded. Additionally, the number of parameters and their types for an operator cannot be changed.

Similar threads

  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
4
Views
777
  • Programming and Computer Science
Replies
2
Views
680
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
1
Views
984
Back
Top