943,942 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1455
  • C RSS
Dec 2nd, 2006
0

Pointers to class members - it just won't work.

Expand Post »
Short example of problem I have:

  1. class A{
  2. public:
  3. int x[10];
  4. };
  5.  
  6. int main(){
  7. int A::*pointer = &A::x;
  8.  
  9. A a;
  10. a.*pointer = 3;
  11. return 0;
  12. }
I got an error: cannot convert from 'int (A: )[10]' to 'int A: ' in second line of 'main()'

I tried to remove '&' from this line because x is an array so the name of array (without square brackets) like A::x should return it's adress (in this case shift in A class), but now I got an error like this:

illegal reference to non-static member 'A::x'

Why compiler thinks I want to convert something? I just want to create a pointer to int array x[10], member of A class.
Could you help?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster
edek is offline Offline
113 posts
since Oct 2006
Dec 2nd, 2006
0

Re: Pointers to class members - it just won't work.

You're not doing it correctly -- you might want to try something like:
  1. A a;
  2. int *pointer;
  3. pointer=a.x;
I haven't actually tried this code out, so it may not compile.
Last edited by John A; Dec 2nd, 2006 at 7:36 pm.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Dec 2nd, 2006
0

Re: Pointers to class members - it just won't work.

OK, but it is not what I'm trying to do.
I do not need a pointer to an member of object 'a' of type 'A'

I need a pointer to class member which can be created even before the object of such class is created and then used with this object, like this:

  1. int A::*pointer = A::x; //main problem: how to get adress (shift in class A) of member x
  2. //A::x doesn't return adress (shift) that I need
  3. A a;
  4. a.*pointer = 5
  5.  
  6. A b;
  7. b.*pointer = 33;
  8. etc.
But it doesnt work with this syntax, any other idea?
Last edited by edek; Dec 2nd, 2006 at 7:31 pm.
Reputation Points: 10
Solved Threads: 0
Junior Poster
edek is offline Offline
113 posts
since Oct 2006
Dec 2nd, 2006
0

Re: Pointers to class members - it just won't work.

>>a.*pointer
that is incorrect syntax -- remove the star and add class member named pointer.
class A{
public:
    int x[10];
    int* pointer;
};

A a;
a.pointer = &a.x[0];
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Dec 3rd, 2006
0

Re: Pointers to class members - it just won't work.

not just that, but you DO get an object instance when you do "A a;".
It's just created in a different memory space as compared to "A a = new A();".
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 3rd, 2006
0

Re: Pointers to class members - it just won't work.

I feel I was not understood again, propably my fault. If you would like to see what I ment see:
http://www.informit.com/guides/conte...eqNum=142&rl=1
Last edited by edek; Dec 3rd, 2006 at 10:57 am.
Reputation Points: 10
Solved Threads: 0
Junior Poster
edek is offline Offline
113 posts
since Oct 2006
Dec 3rd, 2006
0

Re: Pointers to class members - it just won't work.

>I feel I was not understood again, propably my fault.
Don't worry about it. Pointers to members aren't exactly a commonly used feature. The problem in your initial code is a type mismatch. x is an array, but to get a pointer to it, you need a pointer to an array member and you were only using a pointer to an int member:
  1. class A{
  2. public:
  3. int x[10];
  4. };
  5.  
  6. int main(){
  7. int (A::*pointer)[10] = &A::x;
  8.  
  9. A a;
  10. (a.*pointer)[0] = 3;
  11. return 0;
  12. }
Unfortunately, this isn't a case where you can rely on the implicit conversion of an array name to a pointer to the first element of the array. To do that you would need to add another level of abstraction:
  1. class A{
  2. public:
  3. int x[10];
  4. };
  5.  
  6. int main(){
  7. int (A::*pointer)[10] = &A::x;
  8. int *p;
  9. A a;
  10.  
  11. p = a.*pointer;
  12. *p = 3;
  13.  
  14. return 0;
  15. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 3rd, 2006
0

Re: Pointers to class members - it just won't work.

Thank you very much for understanding me! Bye.
Reputation Points: 10
Solved Threads: 0
Junior Poster
edek is offline Offline
113 posts
since Oct 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: Reading a file line by line problem
Next Thread in C Forum Timeline: Explaining the "0x30"





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


Follow us on Twitter


© 2011 DaniWeb® LLC