Jump to content

  • Log In with Google      Sign In   
  • Create Account


c++ pointer to member array of int


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 noatom   Members   -  Reputation: 782

Like
0Likes
Like

Posted 04 April 2013 - 02:20 PM

class x{
public:
int arr[10];
};

 

How do you create a pointer to arr?



Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8012

Like
1Likes
Like

Posted 04 April 2013 - 02:26 PM

x xObj;
int *p = xObj.arr;


#3 noatom   Members   -  Reputation: 782

Like
1Likes
Like

Posted 04 April 2013 - 02:30 PM

No,i mean with the special syntax for pointer to member,like:

 

x d

x* z = &d;

int x::*mypointer = &x::arr;

 

the above would work if arr would be a simple int,not an array



#4 Brother Bob   Moderators   -  Reputation: 8012

Like
7Likes
Like

Posted 04 April 2013 - 02:34 PM

I believe it would be like this.

int (x::*mypointer)[10] = &x::arr;


#5 LordRhys   Members   -  Reputation: 349

Like
0Likes
Like

Posted 05 April 2013 - 12:13 PM

This is the hardest part of C++ to me and why I have had so much trouble with it. There are 3 answers to this question and all three show a different way, pointers can just be too confusing.

#6 Brother Bob   Moderators   -  Reputation: 8012

Like
0Likes
Like

Posted 05 April 2013 - 12:25 PM

Yes, pointers can be confusing. But there was only one answer to this question, not three. My first response, for example, was not the kind of pointer he was looking for and thus is not an answer. Member pointers are different from pointers. My second response is the only answer in this case if he wants a member pointer to an array of 10 integers.


Edited by Brother Bob, 05 April 2013 - 12:26 PM.


#7 King Mir   Members   -  Reputation: 1945

Like
0Likes
Like

Posted 05 April 2013 - 04:42 PM

Also, if the wrap around syntax of array types is confusing, you can use a typedef to define the pointer type in parts:

typedef int IntArray10[10];
IntArray10 x::*mypointer = &x::arr;


#8 noatom   Members   -  Reputation: 782

Like
0Likes
Like

Posted 05 April 2013 - 05:11 PM

@LordRhys,don't worry,I've seen a lot of code but I never actually enocuntered those pointers.I asked because I had an exercise about them.You can always make a simple pointer and point it at the array.

 

Even though you won't see many things used in c++,it's good to have an ideea about them,because you might encounter them once.



#9 Paradigm Shifter   Crossbones+   -  Reputation: 5252

Like
0Likes
Like

Posted 05 April 2013 - 05:17 PM

I've used pointer to member functions but not pointer to member variables. It's pretty rare.

 

Basically it's just an offset and may be useful if you want to point to one of several arrays in an object... Still pretty much a borderline case though...

 

I'm not even sure if the syntax is any better than the (unsafe) C way of just calculating the offset. I supose it's safer but if I was doing anything like that it would be low level and wrapped in a class anyway...


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS