a problem about the pointer to class member

M

miaohua1982

the code is as follows:

#include<iostream>
using namespace std;

class A
{
public:
int a;
int b;
};

int main(int argc,char **argv)
{
int (A::*pInt) = &A::a;
if ( &A::a )
{
cout<<"OK1n"<<endl;
}
if( pInt)
{
cout<<"OK2"<<endl;
}
return 0;
}

I run the code in VC7 , VC8
the problem is why there is only out put "OK2"?
 
O

Ondra Holub

(e-mail address removed) napsal:
the code is as follows:

#include<iostream>
using namespace std;

class A
{
public:
int a;
int b;
};

int main(int argc,char **argv)
{
int (A::*pInt) = &A::a;
if ( &A::a )
{
cout<<"OK1n"<<endl;
}
if( pInt)
{
cout<<"OK2"<<endl;
}
return 0;
}

I run the code in VC7 , VC8
the problem is why there is only out put "OK2"?

I do not know why (maybe bug?). In g++ it works. You can change it to
if ( &A::a != 0) and it should work.
 
M

miaohua1982

yes if ( &A::a != 0) does work, so another bug in VC8?
"Ondra Holub дµÀ£º
"
 
J

Jim Langston

the code is as follows:

#include<iostream>
using namespace std;

class A
{
public:
int a;
int b;
};

int main(int argc,char **argv)
{
int (A::*pInt) = &A::a;
if ( &A::a )
{
cout<<"OK1n"<<endl;
}
if( pInt)
{
cout<<"OK2"<<endl;
}
return 0;
}

I run the code in VC7 , VC8
the problem is why there is only out put "OK2"?

if ( &A::a )
{
std::cout << "OK1n" << std::endl;
}

warning C4127: conditional expression is constant

if ( &(A::a) )
{
std::cout << "OK1n" << std::endl;
}
error C2597: illegal reference to non-static member 'A::a'

That should tell you what's going on right there.

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 )
{
std::cout << "OK3" << std::endl;
}

Compiles fine.

What are you trying to accomplish by taking the address of a non-instantized
class definition?
 
O

Ondra Holub

(e-mail address removed) napsal:
yes if ( &A::a != 0) does work, so another bug in VC8?
"Ondra Holub 写é“:

I would say yes, it is probably bug. I tried it in Borland®
C++Builder® for Microsoft® Windows™ Version 10.0.2288.42451 too and
it works without problems.
 
O

Ondra Holub

Jim Langston napsal:
if ( &A::a )
{
std::cout << "OK1n" << std::endl;
}

warning C4127: conditional expression is constant

if ( &(A::a) )
{
std::cout << "OK1n" << std::endl;
}
error C2597: illegal reference to non-static member 'A::a'

That should tell you what's going on right there.

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 )
{
std::cout << "OK3" << std::endl;
}

Compiles fine.

What are you trying to accomplish by taking the address of a non-instantized
class definition?

It may be used together with some instance (although I never used
anything like that):

#include <iostream>

class A
{
public:
int a;
int b;
};

int main()
{
int (A::*pInt) = &A::a;
A a1;
A a2;

a1.*pInt = 5;
a2.*pInt = 12;
std::cout << a1.a << ' ' << a2.a << '\n';
}
 
M

miaohua1982

the expression of &A::a does not mean to get the address of member a,
but to get its offset in class A,

By section 5.3.1/2 of the C++ standard:

<quote>
The result of the unary & operator is a pointer to its operand...If the

member is a nonstatic member of class C of type T, the type of the
result is
"pointer to member of class C of type T."
[Example:
struct A { int i; };
struct B : A { };
.... &B::i ... // has type int A::*
-end example]
</quote>



"Jim Langston дµÀ£º
"
 
M

miaohua1982

I don't think so , how about the following code?
#include<iostream>
using namespace std;

class A
{
public:
int a;
int b;
};

int main(int argc,char **argv)
{
if (&A::a )
{
cout<<"OK\n";
}
cout<<&A::a<<endl;
return 0;
}

the output is 1 in vc7, which means in "cout" , it does the correct
check, but why not the "if" expression

"Ondra Holub дµÀ£º
"
 
G

Grizlyk

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top