29

Can someone please explain this to me

dynamic_cast<SomeObject *>( &(*similarObject) );

What is the point of doing the address of a dereferenced pointer? Wouldn’t the pointer itself just be the address of it?

Nawaz
  • 353,942
  • 115
  • 666
  • 851
poco
  • 2,935
  • 6
  • 37
  • 54
  • 7
    Is similarObject a smart pointer? This technique is sometimes used to get the reference of a raw pointer, when * has been overloaded. – DanDan Aug 05 '11 at 15:11
  • Are the dereference and address-of operator overloaded perhaps? – Kerrek SB Aug 05 '11 at 15:13
  • 3
    @DanDan: Put this as an answer. –  Aug 05 '11 at 15:14
  • 4
    &* is also known as "unsmart" operator. Parenthesis are unnecessary. – Maxim Egorushkin Aug 05 '11 at 15:16
  • Is `similarObject` is a null pointer, this invokes undefined behavior (at least in the current standard). Also, if `similarObject` is a shared pointer or something like that, you introduce a potential leak (there is `dynamic_pointer_cast` for this use). Also `someObject` could be an iterator, not a pointer. – Alexandre C. Aug 05 '11 at 15:17
  • @DanDan someObject is a user defined class – poco Aug 05 '11 at 15:33

3 Answers3

32

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.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 1
    I sometimes use `&*v.begin()` instead of `&v[0]` to get a pointer to the beginning of a `std::vector`. – Alexandre C. Aug 05 '11 at 15:22
  • 1
    @Alexandre: In that case, I find `&v[0]` easy to understand, and so I prefer it over `&*v.begin()`. – Nawaz Aug 05 '11 at 15:23
  • I used to find `&v[0]` easier too, but not anymore. Tastes change with time. – Alexandre C. Aug 05 '11 at 15:25
  • @Alexandre: You mean you used to find `&v[0]` easier, then you got an accident and you started finding it difficult? :P :D – Nawaz Aug 05 '11 at 15:26
  • square brackets are difficult to type on French keyboards, and using `v.begin()` consistantly makes me happy :). The point is that the "`&*` operator" has some use. – Alexandre C. Aug 05 '11 at 15:27
  • @Alexandre: Hmm.. I see. I didn't know that. I don't know French. Thanks for the info. :-) – Nawaz Aug 05 '11 at 15:30
  • @Alexandre, @Nawaz: I use this form only to get the first item of a `set`, or a similar data structure that does not provide a `[]` or a `front` method. I do not find `[]` that difficult to type, though I do use a French keyboard. – Matthieu M. Aug 05 '11 at 16:04
  • Also of note is that for any pointer `x`, `x` may be an lvalue, but `&*x` can never be an lvalue. Though that's not relevant in OP's example, since the return value of `dynamic_cast<>` isn't an lvalue either. – Adam Rosenfield Oct 08 '11 at 18:24
17

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

DanDan
  • 10,462
  • 8
  • 53
  • 69
1

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

fredoverflow
  • 256,549
  • 94
  • 388
  • 662