When to use addressof(x) instead of &x?

前端 未结 5 504
清歌不尽
清歌不尽 2020-12-24 04:53

How do I decide whether I need addressof(x) instead of &x when taking the address of an object?


Seems like the question was confu

5条回答
  •  心在旅途
    2020-12-24 05:09

    My opinion only:

    Unless you are part of the team designing the class and its interface, never. I've personally never seen a good reason to overload that operator. But if someone designs a class where it makes sense, and assuming the class is meant for public consumption(that is to say, the class is not for internal use only in a particular library), I will expect the class to work in normal code that naturally expects that & means "address of". If the class that overloads the operator isn't designed in such a sensible way, then I wouldn't use that class, period. Because either that class is broken, or it is not meant to be used outside the library which it is a part of.

提交回复
热议问题