[英]When overloading operator << inside a class, why I must declare it as a friend function despite the fact that it is already declared inside the class?
I am trying to overload operator '<<' inside a class, but it's a must to declare it as a friend function otherwise the compiler would through into error: binary'operator<<' has too many parameters This is my function prototype:我正在尝试在 class 中重载运算符'<<',但必须将其声明为朋友function 否则编译器会出错: binary'operator<<' has too many parameters这是我的 ZC1C42145074C68394D
ostream& operator<< (ostream& sout, const SimpleVector v);
I understand that friend function is used if I want to implement the function outside the class and access non-public class member, but this time I am implementing the function inside the class why do I need to use friend keyword I understand that friend function is used if I want to implement the function outside the class and access non-public class member, but this time I am implementing the function inside the class why do I need to use friend keyword
If you really want to define the stream operator inside your class you need to omit the argument const SimpleVector v
because it is already in the this
pointer.如果您真的想在 class 中定义 stream 运算符,则需要省略参数
const SimpleVector v
因为它已经在this
指针中。 However with that you cannot use the operator as usual (because of missing uniform function call syntax ):但是,您不能像往常一样使用运算符(因为缺少统一的 function 调用语法):
std::cout << SimpleVector() << std::endl;
because this calls the free function因为这调用了免费的 function
operator<<(std::ostream&, const SimpleVector);
You would need to call the member function in a cumbersome way:您需要以繁琐的方式调用成员 function:
SimpleVector v;
v.operator<<(std::cout) << std::endl;
Npn-static member functions are considered to have an extra parameter, called the implicit object parameter, which represents the object for which member functions have been called. Npn-static 成员函数被认为有一个额外的参数,称为隐式 object 参数,它代表已调用成员函数的 object。 There is a convention that the implicit object parameter, if present, is always the first parameter and the implied object argument, if present, is always the first argument.
有一个约定,隐含的 object 参数(如果存在)始终是第一个参数,而隐含的 object 参数(如果存在)始终是第一个参数。
So this declaration within a class所以这个声明在 class
ostream& operator<< (ostream& sout, const SimpleVector v);
implies that the function has three parameters while the overloaded operator <<
is a binary operator.意味着 function 具有三个参数,而重载
operator <<
是二元运算符。
When such a function is declared as a friend function then neither implicit parameter that refers to an object of a class is present.当这样的 function 被声明为朋友 function 时,则不会出现指向 object 的 ZA2C212ED4F8EBC2CBBBB1 的隐式参数。
I understand that friend function is used if I want to implement the function outside the class
我知道如果我想在 class 之外实现 function ,则使用朋友 function
You may implement a friend function within a class definition.您可以在 class 定义中实现朋友 function。
Functions are declared as friend functions to make it possible to access private and protected members of classes within friend functions.函数被声明为友元函数,以便可以访问友元函数中类的私有和受保护成员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.