简体   繁体   中英

Why have to use friend function

I am trying to overload operator '=' and operator '<<' by the same method.

class Vect{
 public:
  //..
   Vect& operator=(const Vect& a);      
   ostream& operator<<(ostream& out, const Vect& vect);
  //..
 private:
  int *data;
  int size;
};

this work

Vect& Vect:: operator=(const Vect& a){
 //.. 
 //copy data operator
 for(int i = 0; i< size; i++){
  data[i] = a.data[i];
 }
 return *this;
}

however: this code cause error
[Error] 'std::ostream& Vect::operator<<(std::ostream&, const Vect&)' must take exactly one argument

ostream& Vect::operator<<(ostream& out, const Vect& vect){
 //.. print vect
}

I am reading "Data structure and algorithms in C++" book part (1.5.4). They said i have to use class friends for overloading '<<' operator because it is access private members data . I don't understand why. Overloading '=' operator i also access private member data without using "friend".

When you put a function declaration inside the class definition, it becomes by default a member function, so...

class Vect {
  public:
    ostream& operator<<(ostream& out, const Vect& vect);
};

...won't compile as it requests creation of a << function that takes too many arguments arguments: any member function operator<< is expected to use *this as the "left hand side" argument, and take one other argument to be the "right hand side".

You have two options:

  • Replace the above with friend ostream& operator<<(ostream& out, const Vect& vect); , which tells the compiler that the function is a friend of the surrounding class, but not a member thereof. As a non-member, the two arguments it operates on are out and vect - there is no *this object involved. That all works fine, and being a friend the definition also has access to private and protected member data in vect .

  • Move the operator<< declaration outside the class Vect definition; that also makes it a non-member function, but that doesn't make it a friend.

Overloading '=' operator i also access private member data without using "friend"

operator= is declared as a member function, which can access the private member.

They said i have to use class friends for overloading '<<' operator because it is access private members data. I don't understand why.

The problem is you're declaring operator<< as a member function, which should be declared as a non-member function. That's why you got the error.

operator<< should be a non-member function, because it needs a different type as its left-hand argument, ie std::ostream& , instead of Vect& when declared as a member function.

That means, as a non-member function, operator<< couldn't access the private member of the class unless you declare it as friend, such as:

class Vect {
public:
  //..
    friend ostream& operator<<(ostream& out, const Vect& vect);
  //..

Note the above syntax makes operator<< a non-member function now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM