Pointer to a data member

Reply

Join Date: Jun 2006
Posts: 89
Reputation: dilip.mathews is an unknown quantity at this point 
Solved Threads: 3
dilip.mathews's Avatar
dilip.mathews dilip.mathews is offline Offline
Junior Poster in Training

Pointer to a data member

 
0
  #1
Jun 30th, 2006
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);
}


Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Pointer to a data member

 
0
  #2
Jun 30th, 2006
Where are you getting this from?
  1. int A::*p = &(A::d);
In C you declare Type <variable name> ;
so to declare a pointer to an integer using the * dereference symbol you do this:
  1. int * p = &someintegervariable

try this:
  1. #include <iostream>
  2.  
  3. class A
  4. {
  5. public:
  6. int x;
  7. };
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11. A a;
  12. a.x = 10;
  13. int * p = &a.x;
  14. std::cout << "Memory = " << p << "\n";
  15. std::cout << "Value pointed to = " << *p << "\n";
  16. std::cin.get();
  17. return 0;
  18. }

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.
Last edited by hollystyles; Jun 30th, 2006 at 8:52 am.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 89
Reputation: dilip.mathews is an unknown quantity at this point 
Solved Threads: 3
dilip.mathews's Avatar
dilip.mathews dilip.mathews is offline Offline
Junior Poster in Training

Re: Pointer to a data member

 
0
  #3
Jun 30th, 2006
Originally Posted by hollystyles
Where are you getting this from?
  1. int A::*p = &(A::d);
In C you declare Type <variable name> ;
so to declare a pointer to an integer using the * dereference symbol you do this:
  1. int * p = &someintegervariable

try this:
  1. #include <iostream>
  2.  
  3. class A
  4. {
  5. public:
  6. int x;
  7. };
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11. A a;
  12. a.x = 10;
  13. int * p = &a.x;
  14. std::cout << "Memory = " << p << "\n";
  15. std::cout << "Value pointed to = " << *p << "\n";
  16. std::cin.get();
  17. return 0;
  18. }

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)
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Pointer to a data member

 
0
  #4
Jun 30th, 2006
'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 ?
Last edited by hollystyles; Jun 30th, 2006 at 9:20 am.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Pointer to a data member

 
0
  #5
Jun 30th, 2006
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.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Pointer to a data member

 
1
  #6
Jun 30th, 2006
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):
  1. #include <iostream>
  2.  
  3. class test {
  4. public:
  5. int i;
  6. };
  7.  
  8. int main()
  9. {
  10. int test::*p;
  11. }
Then you need to assign the member you wish to point to by taking its address (also qualified by the class name):
  1. #include <iostream>
  2.  
  3. class test {
  4. public:
  5. int i;
  6. };
  7.  
  8. int main()
  9. {
  10. int test::*p;
  11. p = &test::i;
  12. }
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":
  1. #include <iostream>
  2.  
  3. class test {
  4. public:
  5. int i;
  6. };
  7.  
  8. int main()
  9. {
  10. int test::*p;
  11. p = &test::i;
  12. test a;
  13. a.*p = 12345;
  14. }
This covers pointers in pretty good detail, and there's a bit of coverage on pointers to members.
Last edited by Narue; Jun 30th, 2006 at 9:53 am.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Pointer to a data member

 
1
  #7
Jun 30th, 2006
Ok I get it now.
  1. #include <iostream>
  2.  
  3. class A
  4. {
  5. public:
  6. int x;
  7. };
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11. A a, b;
  12. a.x = 10;
  13. b.x = 20;
  14. int * p = &a.x;
  15. int A::*p2 = &A::x;
  16.  
  17. std::cout << "p points to a.x ONLY @ " << p << "\n";
  18. std::cout << "a.x = " << *p << "\n";
  19. std::cout << "p2 can be used for a.x " << a.*p2 << "\n";
  20. std::cout << "Or for b.x !! wow. " << b.*p2 << "\n";
  21. std::cin.get();
  22. return 0;
  23. }
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 89
Reputation: dilip.mathews is an unknown quantity at this point 
Solved Threads: 3
dilip.mathews's Avatar
dilip.mathews dilip.mathews is offline Offline
Junior Poster in Training

Re: Pointer to a data member

 
0
  #8
Jul 3rd, 2006
Thanks for ur help guys.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC