473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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

Jan 5 '07 #1
8 1786

mi*********@gmail.com 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.

Jan 5 '07 #2
yes if ( &A::a != 0) does work, so another bug in VC8?
"Ondra Holub д
"
mi*********@gmail.com 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.
Jan 5 '07 #3
<mi*********@gmail.comwrote in message
news:11*********************@s80g2000cwa.googlegro 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?
Jan 5 '07 #4

mi*********@gmail.com 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.

Jan 5 '07 #5
Jim Langston napsal:
<mi*********@gmail.comwrote in message
news:11*********************@s80g2000cwa.googlegro 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';
}

Jan 5 '07 #6
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 д
"
<mi*********@gmail.comwrote in message
news:11*********************@s80g2000cwa.googlegro 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?
Jan 6 '07 #7
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:
<mi*********@gmail.comwrote in message
news:11*********************@s80g2000cwa.googlegro 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';
}
Jan 6 '07 #8
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;
}

Jan 12 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
12
by: Aff | last post by:
Brothers , i am facing a problem which is as follow: class poly { private: char name; int capacity; public: poly(char , int );
7
by: Bala L | last post by:
I have a class with a private array data member 'm_array'. I have written a public function, called 'fileRead', to read values into the array from a file. I just noticed that I have declared this...
6
by: Peter Lee | last post by:
what's the correct behaver about the following code ? ( C++ standard ) I got a very strange result.... class MyClass { public: MyClass(const char* p) { printf("ctor p=%s\n", p);
12
by: WaterWalk | last post by:
Hello. I am rather confused by the type of a pointer to class data member. Many c++ texts say that pointer to data member has a special syntax. For example the following class: class MyClass {...
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
5
by: Tim Frink | last post by:
Hi, I'm experimenting with function pointers and found two questions. Let's assume this code: 1 #include <iostream> 2 class A; 3 4 //////////////////////////////////////////// 5 class B
8
by: Daniel T. | last post by:
#include <cassert> class Foo { public: virtual void fnA() = 0; virtual void fnB() = 0; }; int main() { assert( &Foo::fnB );
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.