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

Reply

Join Date: Oct 2006
Posts: 113
Reputation: edek is an unknown quantity at this point 
Solved Threads: 0
edek's Avatar
edek edek is offline Offline
Junior Poster

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

 
0
  #1
Dec 2nd, 2006
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

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

 
0
  #2
Dec 2nd, 2006
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.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 113
Reputation: edek is an unknown quantity at this point 
Solved Threads: 0
edek's Avatar
edek edek is offline Offline
Junior Poster

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

 
0
  #3
Dec 2nd, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,396
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #4
Dec 2nd, 2006
>>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];
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

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

 
0
  #5
Dec 3rd, 2006
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();".
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 113
Reputation: edek is an unknown quantity at this point 
Solved Threads: 0
edek's Avatar
edek edek is offline Offline
Junior Poster

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

 
0
  #6
Dec 3rd, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,630
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: 718
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

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

 
0
  #7
Dec 3rd, 2006
>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. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 113
Reputation: edek is an unknown quantity at this point 
Solved Threads: 0
edek's Avatar
edek edek is offline Offline
Junior Poster

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

 
0
  #8
Dec 3rd, 2006
Thank you very much for understanding me! Bye.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC