Connecting Tech Pros Worldwide Forums | Help | Site Map

a problem about the pointer to class member

miaohua1982@gmail.com
Guest
 
Posts: n/a
#1: Jan 5 '07
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"?


Ondra Holub
Guest
 
Posts: n/a
#2: Jan 5 '07

re: a problem about the pointer to class member



miaohua1982@gmail.com napsal:
Quote:
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.

miaohua1982@gmail.com
Guest
 
Posts: n/a
#3: Jan 5 '07

re: a problem about the pointer to class member


yes if ( &A::a != 0) does work, so another bug in VC8?
"Ondra Holub д
"
Quote:
miaohua1982@gmail.com napsal:
Quote:
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.
Jim Langston
Guest
 
Posts: n/a
#4: Jan 5 '07

re: a problem about the pointer to class member


<miaohua1982@gmail.comwrote in message
news:1167997364.485020.73750@s80g2000cwa.googlegro ups.com...
Quote:
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?


Ondra Holub
Guest
 
Posts: n/a
#5: Jan 5 '07

re: a problem about the pointer to class member



miaohua1982@gmail.com napsal:
Quote:
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.

Ondra Holub
Guest
 
Posts: n/a
#6: Jan 5 '07

re: a problem about the pointer to class member


Jim Langston napsal:
Quote:
<miaohua1982@gmail.comwrote in message
news:1167997364.485020.73750@s80g2000cwa.googlegro ups.com...
Quote:
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?
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';
}

miaohua1982@gmail.com
Guest
 
Posts: n/a
#7: Jan 6 '07

re: a problem about the pointer to class member


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 д
"
Quote:
<miaohua1982@gmail.comwrote in message
news:1167997364.485020.73750@s80g2000cwa.googlegro ups.com...
Quote:
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?
miaohua1982@gmail.com
Guest
 
Posts: n/a
#8: Jan 6 '07

re: a problem about the pointer to class member


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 д
"
Quote:
Jim Langston napsal:
Quote:
<miaohua1982@gmail.comwrote in message
news:1167997364.485020.73750@s80g2000cwa.googlegro ups.com...
Quote:
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?
>
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';
}
Grizlyk
Guest
 
Posts: n/a
#9: Jan 12 '07

re: a problem about the pointer to class member


Jim Langston wrote:
Quote:
Quote:
<miaohua1982@gmail.comwrote in message
class A
{
public:
int a;
int b;
};

int (A::*pInt) = &A::a;
if ( &A::a ){ cout<<"OK1n"<<endl; }
Quote:
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
Quote:
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;
}

Closed Thread