| |

VerySource

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

A question about the overloaded operator +

[Copy link]

3

Threads

9

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 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

49

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 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

49

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 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.00

Credits

Newbie

Rank: 1

Credits
6.00

 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.00

Credits

Newbie

Rank: 1

Credits
9.00

 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

63

Posts

43.00

Credits

Newbie

Rank: 1

Credits
43.00

 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

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 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

2

Threads

20

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 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

1

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 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

39

Posts

27.00

Credits

Newbie

Rank: 1

Credits
27.00

 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-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list