简体   繁体   English

当在 class 中重载运算符 << 时,为什么我必须将其声明为朋友 function,尽管它已经在 class 中声明?

[英]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.

相关问题 在CPP类中声明一个C函数作为朋友 - declare a C function as friend inside CPP class 类内的重载&gt;&gt;运算符 - overloading >> operator inside class 重载类中的&lt;&lt;操作符 - Overloading the << operator inside a class 重载类中的&lt;运算符 - overloading the < operator inside a class 如何在内部 class 内声明与 static function 的朋友? - How to declare friend with static function in Outer class inside the Inner class? 实现“-”运算符重载作为类的友元函数 - Realize "-" operator overloading as a friend function of class 重载运算符&lt;&lt;。 这个操作员不是班级的朋友 - overloading operator<<. This operator is not a friend of class 当你声明一个朋友然后在一个类中定义它时,这意味着什么? - What does it mean when you declare a friend and then define it inside a class? 如何在 class 内声明一个模板的朋友 function 并在 ZA2F221ED4F8EBC29DCBDC4 外部实现这个朋友 function - How to declare a friend function of a template inside a class and implement this friend function ouside class? 当我使用已在类外声明并稍后在类内声明的名称时,这是未定义的行为还是非法行为? - Is it undefined behavior or illegal, when I use a name that is both already declared outside of the class and later declared inside the class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM