Jim Langston wrote:
<mi*********@gmail.comwrote in message
class A
{
public:
int a;
int b;
};
int (A::*pInt) = &A::a;
if ( &A::a ){ cout<<"OK1n"<<endl; }
warning C4127: conditional expression is constant
Condition ( &A::a ) is always false or true due to class A declaration.
I supposed, "A::a" has offset 0, so condition ( &A::a ) is always
false, but i was wrong.
if ( &A::a ){ cout<<"OK1n"<<endl; }
compiled to
movl $0, %eax //&A::a
cmpl $-1, %eax //if(&A::a)
je //else
a is a non static variable of the class. You are trying to take the address
of something that doesn't exist. a won't have an address until A is
instantized.
A Foo;
if ( &Foo.a )
"&Foo.a" is not the same to "&A::a". &A::a always exist if class A was
defined.
Take it
{
union
{
int (A::*pInt);
int tmp;
} x;
A y;
cout<< "&y.a= "<< &y.a<< endl;
x.pInt = &A::a;
cout<< "&A::a"<< "tmp= "<< x.tmp<< endl;
cout<< "&A::a"<< "pInt= "<< x.pInt<< endl;
if ( &A::a ) { cout<<"OK1"<<endl; }
if ( x.pInt ) { cout<<"OK2"<<endl; }
cout<< endl;
cout<< "&y.b= "<< &y.b<< endl;
x.pInt = &A::b;
cout<< "&A::b"<< "tmp= "<< x.tmp<< endl;
cout<< "&A::b"<< "pInt= "<< x.pInt<< endl;
if ( &A::a ) { cout<<"OK3"<<endl; }
if ( x.pInt ) { cout<<"OK4"<<endl; }
cout<< endl;
x.tmp = -1;
cout<< "set -1 "<< "tmp= "<< x.tmp<< endl;
cout<< "set -1 "<< "pInt= "<< x.pInt<< endl;
if ( x.pInt ) { cout<<"OK5"<<endl; }
cout<< endl;
}