| |

VerySource

 Forgot password?
 Register
Search
View: 2067|Reply: 14

The problem of == in vc ++, waiting online

[Copy link]

1

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-2-6 15:00:01
| Show all posts |Read mode
If I define class A, I define two objects as follows:
A a, b;
if (a == b) {
}
Is it OK to write code like this?
Is it wrong to compile a == b here?
why?
Reply

Use magic Report

1

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-3-25 19:30:01
| Show all posts
Overloaded operator
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-25 20:15:01
| Show all posts
It will go wrong, if you want to use it like this, you need to define the "==" operator for your class.
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-25 22:30:02
| Show all posts
Overloaded == operator

bool A :: operator == (const A&ins)
Reply

Use magic Report

0

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-3-26 00:15:01
| Show all posts
a, b are the variables of the class, there is no comparison function in the class, you need to write an overloaded function comparison yourself. Just like a = b, call the Cobb function. Just like if CArray <int, int> m_IDArr is defined in class A, then calling a = b will cause an error, because m_IDArr has no test function, so you must define the test function yourself.
Reply

Use magic Report

1

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-3-26 09:30:01
| Show all posts
If I overload =, but not overload ==, I
A a, b;
if (a == b) {
}

Can this be written?
Reply

Use magic Report

1

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-3-26 12:00:01
| Show all posts
If so, why?
Reply

Use magic Report

1

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-3-26 20:30:01
| Show all posts
= Not ==
Reply

Use magic Report

0

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-3-27 00:15:01
| Show all posts
The language foundation is so poor, I have got so many triangles

Does mfc really need language support ^-^
Reply

Use magic Report

1

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-3-27 23:15:01
| Show all posts
The 2 masters upstairs have tried overloading = not overloading ==, is it definitely not working?
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