Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old January 5th, 2007, 11:55 AM
miaohua1982@gmail.com
Guest
 
Posts: n/a
Default a problem about the pointer to class member

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"?

  #2  
Old January 5th, 2007, 12:25 PM
Ondra Holub
Guest
 
Posts: n/a
Default 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.

  #3  
Old January 5th, 2007, 12:45 PM
miaohua1982@gmail.com
Guest
 
Posts: n/a
Default 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.
  #4  
Old January 5th, 2007, 08:15 PM
Jim Langston
Guest
 
Posts: n/a
Default 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?


  #5  
Old January 5th, 2007, 08:25 PM
Ondra Holub
Guest
 
Posts: n/a
Default 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.

  #6  
Old January 5th, 2007, 08:35 PM
Ondra Holub
Guest
 
Posts: n/a
Default 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';
}

  #7  
Old January 6th, 2007, 04:05 AM
miaohua1982@gmail.com
Guest
 
Posts: n/a
Default 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?
  #8  
Old January 6th, 2007, 04:05 AM
miaohua1982@gmail.com
Guest
 
Posts: n/a
Default 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';
}
  #9  
Old January 12th, 2007, 07:35 AM
Grizlyk
Guest
 
Posts: n/a
Default 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;
}

 

Bookmarks

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles