| | |
Pointers to class members - it just won't work.
![]() |
Short example of problem I have:
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?
C Syntax (Toggle Plain Text)
class A{ public: int x[10]; }; int main(){ int A::*pointer = &A::x; A a; a.*pointer = 3; return 0; }
)[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?
You're not doing it correctly -- you might want to try something like:
I haven't actually tried this code out, so it may not compile.
C Syntax (Toggle Plain Text)
A a; int *pointer; pointer=a.x;
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."
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:
But it doesnt work with this syntax, any other idea?
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:
C Syntax (Toggle Plain Text)
int A::*pointer = A::x; //main problem: how to get adress (shift in class A) of member x //A::x doesn't return adress (shift) that I need A a; a.*pointer = 5 A b; b.*pointer = 33; etc.
Last edited by edek; Dec 2nd, 2006 at 7:31 pm.
>>a.*pointer
that is incorrect syntax -- remove the star and add class member named 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.
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
http://www.informit.com/guides/conte...eqNum=142&rl=1
Last edited by edek; Dec 3rd, 2006 at 10:57 am.
>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:
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:
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:
C Syntax (Toggle Plain Text)
class A{ public: int x[10]; }; int main(){ int (A::*pointer)[10] = &A::x; A a; (a.*pointer)[0] = 3; return 0; }
C Syntax (Toggle Plain Text)
class A{ public: int x[10]; }; int main(){ int (A::*pointer)[10] = &A::x; int *p; A a; p = a.*pointer; *p = 3; return 0; }
I'm here to prove you wrong.
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: Reading a file line by line problem
- Next Thread: Explaining the "0x30"
| Thread Tools | Search this Thread |
#include * adobe ansi array asterisks binarysearch centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc database dynamic execv feet fgets file floatingpointvalidation fork function getlogicaldrivestrin givemetehcodez global grade gtkwinlinux hacking histogram ide inches include incrementoperators infiniteloop input interest intmain() iso kernel keyboard kilometer license linked linkedlist linux list locate looping lowest matrix meter microsoft mqqueue number oddnumber opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer posix power probleminc process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprograming socketprogramming standard strchr string suggestions systemcall test threads turboc unix urboc user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






