Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > a problem about the pointer to class member

Reply
Thread Tools

a problem about the pointer to class member

 
 
miaohua1982@gmail.com
Guest
Posts: n/a
 
      01-05-2007
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"?

 
Reply With Quote
 
 
 
 
Ondra Holub
Guest
Posts: n/a
 
      01-05-2007

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.

 
Reply With Quote
 
 
 
 
miaohua1982@gmail.com
Guest
Posts: n/a
 
      01-05-2007
yes if ( &A::a != 0) does work, so another bug in VC8?
"Ondra Holub д
"
> 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.


 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      01-05-2007
<> wrote in message
news: ups.com...
> 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?


 
Reply With Quote
 
Ondra Holub
Guest
Posts: n/a
 
      01-05-2007

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.

 
Reply With Quote
 
Ondra Holub
Guest
Posts: n/a
 
      01-05-2007
Jim Langston napsal:
> <> wrote in message
> news: ups.com...
> > 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';
}

 
Reply With Quote
 
miaohua1982@gmail.com
Guest
Posts: n/a
 
      01-06-2007
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 д
"
> <> wrote in message
> news: ups.com...
> > 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?


 
Reply With Quote
 
miaohua1982@gmail.com
Guest
Posts: n/a
 
      01-06-2007
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 д
"
> Jim Langston napsal:
> > <> wrote in message
> > news: ups.com...
> > > 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';
> }


 
Reply With Quote
 
Grizlyk
Guest
Posts: n/a
 
      01-12-2007
Jim Langston wrote:

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer-to-member data and pointer-to-member functions and access specifiers Stephen Howe C++ 2 11-06-2012 12:32 PM
What is the correct grammar to make a function call by using static member data which is a pointer to a ordinary class member function? zaeminkr@gmail.com C++ 3 07-06-2007 12:50 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Pointer-to-member-function pointing to a member function of an inherited class akiriwas@gmail.com C++ 12 02-11-2005 05:15 PM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM



Advertisments