Difference between &(*similarObject) and similarObject? Are they not same?

后端 未结 3 1514
谎友^
谎友^ 2020-12-06 04:09

Can someone please explain this to me

dynamic_cast( &(*similarObject) );

What is the point of doing the address of

相关标签:
3条回答
  • 2020-12-06 04:47

    Nobody mentioned yet that similarObject is an lvalue, whereas &*similarObject is an rvalue.

    0 讨论(0)
  • 2020-12-06 04:57

    It may be that the type of similarObject has overloaded operator* and so it returns something whose address you're passing to dynamic_cast.

    &(*x) and x may not be always the same thing. For example, think of iterator:

    std::map<int, int>::iterator it = v.begin();
    

    Then it and &(*it) are two different thing:

    • The type of it is std::map<int, int>::iterator
    • The type of &(*it) is std::pair<int,int>*

    They're not at all same. Similar thing may happen with your code-snippet as well.

    0 讨论(0)
  • 2020-12-06 04:57

    If similarObject is a smart pointer, this technique is sometimes used to get the reference of a raw pointer, when * has been overloaded.

    0 讨论(0)
提交回复
热议问题