zpz / zpz.github.io

blog
http://zpz.github.io
MIT License
1 stars 0 forks source link

Cast line in sample code will result in a copy. #46

Open jovere opened 6 months ago

jovere commented 6 months ago

Hi Zepu,

I realize this is quite old, but I was recently looking at your post regarding overloading the == operator. I found it quite useful. However, in the _equals functions, there is a line similar to the following, which I believe is incorrectly doing a copy:

auto that = static_cast<B const &>(other);

Unfortunately, auto by default does a copy. In order to do a reference, it needs to be explicit:

auto& that = static_cast<B const &>(other);

Other than that, thanks for the great post!

-Jeremy

https://github.com/zpz/zpz.github.io/blob/ccdf73210f2248610b6cfdd713225b2cb4ffd5ca/_posts/2018-01-27-overloading-equality-operator-in-cpp-class-hierarchy.md?plain=1#L124

zpz commented 6 months ago

Hi Jeremy, thanks for pointing this out! This is a significant issue. It's a few years since I last used C++. I will make a correction to the post at some point, with full credit to you.

Best, Zepu

On Thu, Apr 4, 2024, 11:29 AM Jeremy Overesch @.***> wrote:

Hi Zepu,

I realize this is quite old, but I was recently looking at your post regarding overloading the == operator. I found it quite useful. However, in the _equals functions, there is a line similar to the following, which I believe is incorrectly doing a copy:

auto that = static_cast<B const &>(other);

Unfortunately, auto by default does a copy. In order to do a reference, it needs to be explicit:

auto& that = static_cast<B const &>(other);

Other than that, thanks for the great post!

-Jeremy

https://github.com/zpz/zpz.github.io/blob/ccdf73210f2248610b6cfdd713225b2cb4ffd5ca/_posts/2018-01-27-overloading-equality-operator-in-cpp-class-hierarchy.md?plain=1#L124

— Reply to this email directly, view it on GitHub https://github.com/zpz/zpz.github.io/issues/46, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABGPRODRGJ7CL66N3JVYVXDY3WEXHAVCNFSM6AAAAABFXWNZOCVHI2DSMVQWIX3LMV43ASLTON2WKOZSGIZDMMJSHEZTAMQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

zpz commented 4 months ago

Hi Jeremy, I've just added your correction to the end of that post. Thanks again!

Zepu

On Thu, Apr 4, 2024 at 10:29 AM Jeremy Overesch @.***> wrote:

Hi Zepu,

I realize this is quite old, but I was recently looking at your post regarding overloading the == operator. I found it quite useful. However, in the _equals functions, there is a line similar to the following, which I believe is incorrectly doing a copy:

auto that = static_cast<B const &>(other);

Unfortunately, auto by default does a copy. In order to do a reference, it needs to be explicit:

auto& that = static_cast<B const &>(other);

Other than that, thanks for the great post!

-Jeremy

https://github.com/zpz/zpz.github.io/blob/ccdf73210f2248610b6cfdd713225b2cb4ffd5ca/_posts/2018-01-27-overloading-equality-operator-in-cpp-class-hierarchy.md?plain=1#L124

— Reply to this email directly, view it on GitHub https://github.com/zpz/zpz.github.io/issues/46, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABGPRODRGJ7CL66N3JVYVXDY3WEXHAVCNFSM6AAAAABFXWNZOCVHI2DSMVQWIX3LMV43ASLTON2WKOZSGIZDMMJSHEZTAMQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>