简体   繁体   English

C ++重载运算符同时作为成员和函数吗?

[英]C++ Overload Operator as Member and Function at the same time?

C++ Overload Operator as Member and Function at the same time? C ++重载运算符同时作为成员和函数吗? I am trying to set up operator overloading here. 我正在尝试在此处设置运算符重载。

I have no problem with operator+ overloading as friend and member each case. 对于朋友和成员,我对运算符+重载没有任何问题。 But when I try to overload + operator as friend ans the friend as member at the same time I get the error. 但是当我尝试重载+运算符作为朋友,同时又把朋友作为成员时,我得到了错误。

Kind of confused here. 这里有点困惑。 I might try to do something that does not even make sense? 我可能会尝试做什至没有意义的事情? Please check my code and see if I can work it out. 请检查我的代码,看看是否可以解决。 Case 3 is the ERROR generating case. 情况3是产生错误的情况。 Thanks! 谢谢!

CASE1 Overload as Friend: Source.cpp CASE1重载为好友:Source.cpp

#include <iostream>
using namespace std;
#include "Time.h"
int main () {
Time planning;       Time coding(2, 40);
Time fixing(5, 55);  Time total;
total = coding + fixing;
cout << "coding + fixing = ";
total.Show();
cout << endl;
cin.get();  return 0; }

CASE1 Overload as Friend: Time.h CASE1重载为好友:Time.h

#pragma once
class Time
{ int hours; int minutes;
public:
Time(void);
~Time(void);
Time(int, int m = 0);
void AddMin(int);
void AddHr(int);
void Reset(int h=0, int m=0);
friend const Time operator+(const Time&amp;, const Time&amp;);
void Show() const; };

CASE1 Overload as Friend: Time.cpp CASE1重载为好友:Time.cpp

#include "Time.h"
#include <iostream>
using namespace std;
Time::Time(void) { hours = minutes = 0;}
Time::~Time(void) {}
Time::Time (int h, int m) { hours =h; minutes = m; }
void Time::AddMin(int m) {minutes+=m; hours+=minutes/60; minutes%= 60; }
void Time::Reset(int h, int m) {hours = h; minutes = m;}

const Time operator+(const Time &amp; t1, const Time&amp; t2) {
Time sum;
sum.minutes = t1.minutes + t2.minutes;
sum.hours = t1.hours + t2.hours + sum.minutes / 60;
sum.minutes %= 60;
return sum; }
void Time::Show() const
{ cout << hours << " hours, " << minutes << " minutes"; }

CASE2 Overload as Member: Source.cpp / Time.h / Time.cpp Source.cpp // same CASE2作为成员的重载:Source.cpp / Time.h / Time.cpp Source.cpp //相同

Time.h 时间

const Time operator+(const Time&amp;) const;  // declaration

Time.cpp 时间.cpp

const Time Time::operator+(const Time&amp; t) const {
Time sum;
sum.minutes = minutes + t.minutes;
sum.hours = hours + t.hours + sum.minutes / 60;
sum.minutes %= 60;
return sum; }  // function definition

CASE 3: ERROR; 情况 3:错误; trying to overload operator + as friend and member at the same time Source.cpp // same 尝试同时将操作符+重载为朋友和成员Source.cpp //相同

Time.h 时间

friend const Time operator+(const Time&amp;) ;  // declaration

Time.cpp 时间.cpp

friend const Time operator+(const Time&amp; t) {
Time sum;
sum.minutes = minutes + t.minutes;
sum.hours = hours + t.hours + sum.minutes / 60;
sum.minutes %= 60;
return sum; }  // function definition

When you provide an operator overload for your type, what you are doing is providing a function to call when the operator appears in code. 当您为类型提供运算符重载时,您正在做的是提供一个在运算符出现在代码中时要调用的函数。 It does not make sense to provide two definitions when only one can be called and there is no way of selecting it. 当只能调用一个定义且无法选择它时,提供两个定义是没有意义的。

That is, you should not have both a member function and a free function overload. 也就是说,您不应同时具有成员函数和自由函数重载。

The next thing, which might actually be your confusion is what the meaning of friend is. 接下来的一件事,实际上可能是您的困惑,是friend的意思。 When you provide a friend declaration, you are declaring a free function and granting that function access to your private members. 提供朋友声明时,您是在声明一个免费功能,并向该功能授予您的私有成员访问权限。 It is a free function, and as such there is no implicit this argument, thus: 它是一个自由函数,因此没有隐式的this参数,因此:

class X {
   friend X operator+(X const &);
};

would declare an overload for operator+ that takes a single argument of type X const& and returns an object of type X . 会为operator+声明一个重载,该重载采用单个X const&类型的X const&参数并返回X类型的对象。 Incidentally, that is a valid operator, just not the one that you mean to overload (it is the unary operator+ , as in X x; +x; ). 顺便说一句,这是一个有效的运算符,而不是您要重载的运算operator+ (它是一元运算operator+ ,例如X x; +x; )。 Going back to the previous paragraph, it declares a free function, not a member function, there is no implicit this , just like if you had declared: 回到上一段,它声明一个自由函数,而不是一个成员函数,没有隐式this ,就像您已经声明了:

class X {};
X operator+(X const &);

And because there is no implicit this , minutes and hours by themselves don't make sense inside that function and will fail to compile. 而且由于没有隐式this ,因此minuteshours本身在该函数内部没有意义,并且将无法编译。 (The friend keyword in the definition is also wrong, friend can never appear outside of a class definition). (定义中的friend关键字也是错误的, friend永远不能出现在类定义之外)。

There is some confusion as of whether friend has any special meaning with respect to operators, it doesn't. 关于friend对操作员是否有任何特殊含义,这有些困惑,事实并非如此。 It is just a keyword that affects access specifiers. 它只是一个影响访问说明符的关键字。 If you can implement the operator in terms of the public interface of your type, it should not be friend in the first place. 如果您可以根据您的类型的公共接口来实现该运算符,那么它首先不应成为friend So if for example you had accessors in Time to obtain the hours and minutes, you could implement the operator as: 因此,例如,如果您在“ Time有访问器来获取小时和分钟,则可以将运算符实现为:

class Time { // ...
public:
   Time(int hours, int minutes);
   int hours() const;
   int minutes() const;
};
Time operator+(Time const & lhs, Time const & rhs) {
   return Time(lhs.hours()+rhs.hours(), lhs.minutes()+rhs.minutes());
}

With no friendship involved. 没有友谊。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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