944,158 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4538
  • C++ RSS
Jun 30th, 2006
0

Pointer to a data member

Expand Post »
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);
}


Similar Threads
Reputation Points: 16
Solved Threads: 3
Junior Poster in Training
dilip.mathews is offline Offline
89 posts
since Jun 2006
Jun 30th, 2006
0

Re: Pointer to a data member

Where are you getting this from?
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  1. int * p = &someintegervariable

try this:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Jun 30th, 2006
0

Re: Pointer to a data member

Quote originally posted by hollystyles ...
Where are you getting this from?
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  1. int * p = &someintegervariable

try this:
C++ Syntax (Toggle Plain Text)
  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)
Reputation Points: 16
Solved Threads: 3
Junior Poster in Training
dilip.mathews is offline Offline
89 posts
since Jun 2006
Jun 30th, 2006
0

Re: Pointer to a data member

Quote ...
'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.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Jun 30th, 2006
0

Re: Pointer to a data member

Quote ...
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.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Jun 30th, 2006
1

Re: Pointer to a data member

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):
C++ Syntax (Toggle Plain Text)
  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):
C++ Syntax (Toggle Plain Text)
  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":
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 30th, 2006
1

Re: Pointer to a data member

Ok I get it now.
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Jul 3rd, 2006
0

Re: Pointer to a data member

Thanks for ur help guys.
Reputation Points: 16
Solved Threads: 3
Junior Poster in Training
dilip.mathews is offline Offline
89 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: c++ or shell script to delete some files
Next Thread in C++ Forum Timeline: ostringstream overloading





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC