What would be the purpose of using the reference and dereference operators immediately in the "& * B" sequence?

I saw this in our code a couple of times, and it immediately makes me suspicious. But since I do not know the original intention, I do not dare to remove it.

//requires double indirection which I won't go into FooClass::FooFunction(void ** param) { //do something } SomeClass * A = new SomeClass(); SomeClass **B = &A; FooFunction( reinterpret_cast<void**>(&*B) ); // what is happening here? 

Is the "& * B" part the part in question? Feel free to integrate the reinterpretation explanation, but I am well acquainted with casting methods.

+4
source share
3 answers

I see only one reason for this: B overloaded operator*() to return X , but the one who wrote the code needed X* . (Note that there is A* in your X code.) A typical case for this is smart pointers and iterators.

If this is not the case above, perhaps the code was written as general enough to work with smart pointers / iterators. Or did he use smart pointers, and whoever changed it didn’t change &* too? Have you looked at your story when it was introduced and what then did the code look like?

+3
source

I did the same thing with iterators - played the iterator to get the link, and then do "&". operator to get a pointer.

I don’t understand why everything would be done here. If the type to the right of "& *" is a pointer type, it does nothing.

+10
source

Consider the following example:

 class A { public: int f() { return 55; } }; class B { public: B( A* a ) : a(a) {} A*& operator*() { return a; } A* a; }; int main () { A* a = new A(); B b = a; // &* calls operator*, then gets address of A void** x = reinterpret_cast<void**>(&*b); cout << reinterpret_cast<A*>( *x )->f() << endl; // prints 55 void** x2 = reinterpret_cast<void**>( b ); // compile error } 

Your last edit of the question results in:

 A* a = new A(); A** b = &a; void** x = reinterpret_cast<void**>(&*b); // now this is equal to the following void** x2 = reinterpret_cast<void**>( b ); // so using &* make no sense 
+2
source

All Articles