Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is *&x not the same as x?

Short version:

The following code doesn't compile:

CComBSTR temp;
CMenu().GetMenuString(0,   temp, 0);

but this does:

CComBSTR temp;
CMenu().GetMenuString(0, *&temp, 0);

Why?


Full code:

#include <atlbase.h>
extern CComModule _Module;
#include <atlapp.h>
#include <atlwin.h>
#include <atlctrls.h>

int main() {
    CComBSTR temp;
    CMenu().GetMenuString(0, *&temp, 0);
}

GetMenuString signature (from atluser.h, from WTL):

BOOL GetMenuString(UINT nIDItem, BSTR& bstrText, UINT nFlags) const;
like image 542
user541686 Avatar asked Oct 08 '11 20:10

user541686


People also ask

Why is negative into negative positive?

Ideally, the second negative should change the sign of our original number (which is also negative). So, our original negative sign is changed into a positive sign when a negative is multiplied to it. Thus, As multiplying with negative will change the sign, a negative times a negative equal a positive.

Why is negative multiplication positive?

The fact that the product of two negatives is a positive is therefore related to the fact that the inverse of the inverse of a positive number is that positive number back again.

Why is a negative times a negative?

A simpler algebraic proof Using the fact multiplication is commutative, a negative times a positive is also negative. Similarly, we can prove that a negative times a negative is a positive. Since we know that −ab is negative, and the sum of these two terms is 0, therefore (−a) × (−b) is positive.

What's a negative times a positive?

Negative times positive is always negative This means that an expression with one negative number (like multiplying a negative number by a positive one) will always turn out negative. Otherwise, though, multiplying with negative numbers works just like multiplying positive numbers.


2 Answers

Because the unary operators & and * can be overloaded, which I guess CComBSTR does.

* Update: *

For those who wonder how to get the address of a variable whose type has overloaded operator& there is TR1's std::addressof, and a Boost implementation of it for C++03 compatibility.

like image 198
K-ballo Avatar answered Oct 10 '22 07:10

K-ballo


CComBSTR might have overloaded the operator * or operator & to return a type which matches the parameter type received by GetMenuString()

So while *&x is same as x for built-in data types, it may not be the same for user defined types.

like image 24
Alok Save Avatar answered Oct 10 '22 07:10

Alok Save