Pointer to a data member
Hi,
I am bit confused with the concept of pointer to a data member of a class.
In the below program what actually will the pointer variable be storing? When i tried printing the value of address I got value 1, Whatt does it mean?
class A{
public:
int d;
};
void main(void)
{
int A::*p = &(A::d);
}
I tried creating an object of class A and assigning the address of the data member to p then also my VC compiler is throwing error 'cannot convert from 'int *' to 'int A::*''. Why this error is thrown when b is of type A. Same with the gcc compiler also.
class A{
public:
int d;
};
void main(void)
{
A bb;
bb.d = 10;
int A::*p = &(A::d);
p = &(bb.d);
}
dilip.mathews
Junior Poster in Training
89 posts since Jun 2006
Reputation Points: 16
Solved Threads: 3
Where are you getting this from?
int A::*p = &(A::d);
In C you declare Type ;
so to declare a pointer to an integer using the * dereference symbol you do this:
int * p = &someintegervariable
try this:
#include <iostream>
class A
{
public:
int x;
};
int main(int argc, char *argv[])
{
A a;
a.x = 10;
int * p = &a.x;
std::cout << "Memory = " << p << "\n";
std::cout << "Value pointed to = " << *p << "\n";
std::cin.get();
return 0;
}
When we print p we get the value of p itself (which is a memory address where the value of x is being stored) When we print * p we dereference to that address and get the actual bits from that memory location 10 (remember you declare the pointer to be int so the compiler knows to get 4 bytes of memory starting at the address stored in p and it knows the bits stored in those four bytes represent an integer, so cout can diplay them correctly as the decimal integer 10.
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
Where are you getting this from?
int A::*p = &(A::d);
In C you declare Type ;
so to declare a pointer to an integer using the * dereference symbol you do this:
int * p = &someintegervariable
try this:
#include <iostream>
class A
{
public:
int x;
};
int main(int argc, char *argv[])
{
A a;
a.x = 10;
int * p = &a.x;
std::cout << "Memory = " << p << "\n";
std::cout << "Value pointed to = " << *p << "\n";
std::cin.get();
return 0;
}
When we print p we get the value of p itself (which is a memory address where the value of x is being stored) When we print * p we dereference to that address and get the actual bits from that memory location 10 (remember you declare the pointer to be int so the compiler knows to get 4 bytes of memory starting at the address stored in p and it knows the bits stored in those four bytes represent an integer, so cout can diplay them correctly as the decimal integer 10.
What u said is the common way of doingit and I am aware of that.
But what my doubt is if u do something like what I have shown, the compiler will not throw any error. If that is the case, what is the thing which the pointer is storing?
I have seen in some material also that u can access the data member thru this way. (not sure whether this is a right way)
dilip.mathews
Junior Poster in Training
89 posts since Jun 2006
Reputation Points: 16
Solved Threads: 3
'cannot convert from 'int *' to 'int A::*''. Why this error is thrown when b is of type A
Yes b is of type A but you are trying to assign the address of b.d which is an int.
Also A is a class, where as b is an instance of class A. Which are you trying to point to ?
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
I have seen in some material also that u can access the data member thru this way
Is this an internet resource? can you point me to the URL I can't find anything that relates to what you are trying to do.
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
A pointer to a member requires three distinct parts. First, you need to create a pointer (qualified by the class whose member it points to):
#include <iostream>
class test {
public:
int i;
};
int main()
{
int test::*p;
}
Then you need to assign the member you wish to point to by taking its address (also qualified by the class name):
#include <iostream>
class test {
public:
int i;
};
int main()
{
int test::*p;
p = &test::i;
}
Finally, to actually point to an object, you need an existing object and you need to use a member access operator on it using the dereferenced pointer as the "member":
#include <iostream>
class test {
public:
int i;
};
int main()
{
int test::*p;
p = &test::i;
test a;
a.*p = 12345;
}
This covers pointers in pretty good detail, and there's a bit of coverage on pointers to members.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
Ok I get it now.
#include <iostream>
class A
{
public:
int x;
};
int main(int argc, char *argv[])
{
A a, b;
a.x = 10;
b.x = 20;
int * p = &a.x;
int A::*p2 = &A::x;
std::cout << "p points to a.x ONLY @ " << p << "\n";
std::cout << "a.x = " << *p << "\n";
std::cout << "p2 can be used for a.x " << a.*p2 << "\n";
std::cout << "Or for b.x !! wow. " << b.*p2 << "\n";
std::cin.get();
return 0;
}
hollystyles
Veteran Poster
1,182 posts since Feb 2005
Reputation Points: 262
Solved Threads: 68
dilip.mathews
Junior Poster in Training
89 posts since Jun 2006
Reputation Points: 16
Solved Threads: 3