| |

VerySource

 Forgot password?
 Register
Search
View: 12|Reply: 11

A question about the overloaded operator +

[Copy link]

3

Threads

9

Posts

9

Credits

Newbie

Rank: 1

Credits
9

 India

Post time: 2020-3-20 14:00:02
| Show all posts |Read mode
The operator + () function can not be defined as a member function or a friend function. What is the difference between the two cases?
Thank you!
Reply

Use magic Report

0

Threads

38

Posts

27

Credits

Newbie

Rank: 1

Credits
27

 China

Post time: 2020-6-25 23:00:01
| Show all posts
Suppose the class BigInt is a large number processing class
BigInt has BigInt(int i) constructor
BigInt temp;
BigInt temp2 = 2 + temp;
Member functions cannot implicitly convert 2 to BigInt, friend functions can
Reply

Use magic Report

0

Threads

38

Posts

27

Credits

Newbie

Rank: 1

Credits
27

 China

Post time: 2020-6-26 00:30:01
| Show all posts
It should be said that for member function overloading +, this sentence is equivalent to:
BigInt temp2 = 2.operator+(temp);
This is a wrong sentence
Reply

Use magic Report

0

Threads

6

Posts

6

Credits

Newbie

Rank: 1

Credits
6

 China

Post time: 2020-6-26 09:30:01
| Show all posts
Operators that are members have one less parameter in the form of declaration and definition than operators that are not members. This is because c++
The first parameter this is hidden from all member functions

Similarly, the overloaded operator as a member function is also one more than the function parameter as a friend. Take the operator +, for example, in the member function, its first parameter is the object it belongs to, the second Is a parameter, and in the friend function, both objects are parameters
The statement is as follows
Member function version: SomeClass operator + (SomeClass)
Friends function version: friend SomeClass operator + (SomeClass, SomeClass)
Someclass is a class name, assuming that both functions return SomeClass objects.
Reply

Use magic Report

3

Threads

9

Posts

9

Credits

Newbie

Rank: 1

Credits
9

 United States

 Author| Post time: 2020-6-26 15:45:01
| Show all posts
So there are so many reasons inside! Thank you!
Reply

Use magic Report

0

Threads

61

Posts

41

Credits

Newbie

Rank: 1

Credits
41

 Great Britain

Post time: 2020-6-28 16:15:01
| Show all posts
Well, parameters can be implicitly type-converted, an object cannot. It is recommended to look at effective c++ Item19
Reply

Use magic Report

0

Threads

6

Posts

6

Credits

Newbie

Rank: 1

Credits
6

 China

Post time: 2020-6-30 00:45:01
| Show all posts
Member function has a latent parameter.
Friend function does't.
Reply

Use magic Report

0

Threads

11

Posts

7

Credits

Newbie

Rank: 1

Credits
7

 China

Post time: 2020-7-5 14:45:02
| Show all posts
If you want to put the object on the left side of the operator, then you can not use member function
Reply

Use magic Report

0

Threads

1

Posts

2

Credits

Newbie

Rank: 1

Credits
2

 China

Post time: 2020-7-12 22:45:01
| Show all posts
Binocular operator is best to use friend function, member function for monocular
Reply

Use magic Report

1

Threads

29

Posts

19

Credits

Newbie

Rank: 1

Credits
19

 China

Post time: 2020-7-12 23:45:01
| Show all posts
Member definitions have more restrictions, because the default specifications are:
operator+ (int) const -----> operator+ (T const&, int) const;

There are not so many restrictions on friends, you can revise the two parameters separately and make some adjustments, etc.
operator+ (T const&, int)
operator+ (int, T const&)

From the perspective of overloading, friends are more flexible.
However, it is more pleasant to use members or members.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2020|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list