473,398 Members | 2,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Pointer to class member array ... element? Possible?

Given some class C with array T x[N], is it possible to get a
pointer-to-data-member to one of the elements?

&C::x gives us a pointer-to-member-array: T (C::*)[N].

But I just want to get a T C::* pointing to a selected array element,
so that I can later use an instance c of that class to pick out that
array element: c->*ptr.

This syntax, for instance, doesn't work: &C::x[1]. My compiler thinks
I'm trying to do scope resolution to look up x as a member of some base
class: it complains that C isn't a base class of the class that I'm
working in. Of course &C::x works fine. How to sneak in that array
reference? (&C::x)[1] isn't it because then you are doing array
indexing on a pointer to the array, which is invalid; you need an
object to do that.

Is there any way? All I have is this hack:

T (C::*pma)[N] = &C::x; // pointer to member array

T C::*pe1 = (T C::*) ((size_t) pma + sizeof T);

The assumption is that the pointer-to-member is an integer-like offset
that can be converted to a size_t, subject to displacement by a
multiple of the array size and the converted back to pointer-to-member.

Oct 7 '05 #1
3 5822
Kaz Kylheku wrote:
Given some class C with array T x[N], is it possible to get a
pointer-to-data-member to one of the elements?

&C::x gives us a pointer-to-member-array: T (C::*)[N].

But I just want to get a T C::* pointing to a selected array element,
so that I can later use an instance c of that class to pick out that
array element: c->*ptr.

This syntax, for instance, doesn't work: &C::x[1]. My compiler thinks
I'm trying to do scope resolution to look up x as a member of some base
class: it complains that C isn't a base class of the class that I'm
working in. Of course &C::x works fine. How to sneak in that array
reference? (&C::x)[1] isn't it because then you are doing array
indexing on a pointer to the array, which is invalid; you need an
object to do that.

Is there any way? All I have is this hack:

T (C::*pma)[N] = &C::x; // pointer to member array

T C::*pe1 = (T C::*) ((size_t) pma + sizeof T);

The assumption is that the pointer-to-member is an integer-like offset
that can be converted to a size_t, subject to displacement by a
multiple of the array size and the converted back to pointer-to-member.


Please post a minimal but complete sample program.

Cheers! --M

Oct 7 '05 #2
Kaz Kylheku wrote:
Given some class C with array T x[N], is it possible to get a
pointer-to-data-member to one of the elements?
IIUIC, an element of the member array is not a member of that class, so,
no, it should not be possible.
&C::x gives us a pointer-to-member-array: T (C::*)[N].
Right.
But I just want to get a T C::* pointing to a selected array element,
so that I can later use an instance c of that class to pick out that
array element: c->*ptr.
Keep the index instead. Can't you just do c->x[indexyoukeep] ?
This syntax, for instance, doesn't work: &C::x[1]. My compiler thinks
I'm trying to do scope resolution to look up x as a member of some base
class: it complains that C isn't a base class of the class that I'm
working in. Of course &C::x works fine. How to sneak in that array
reference? (&C::x)[1] isn't it because then you are doing array
indexing on a pointer to the array, which is invalid; you need an
object to do that.

Is there any way? All I have is this hack:

T (C::*pma)[N] = &C::x; // pointer to member array

T C::*pe1 = (T C::*) ((size_t) pma + sizeof T);

The assumption is that the pointer-to-member is an integer-like offset
that can be converted to a size_t, subject to displacement by a
multiple of the array size and the converted back to pointer-to-member.


Bad assumption. Imagine what's going to happen when you have multiple
(or, which is worse, virtual) inheritance... Well, I don't actually know
if it's bad, but I just shy away from things like that.

V
Oct 7 '05 #3

"Kaz Kylheku" <kk******@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...

T (C::*pma)[N] = &C::x; // pointer to member array

T C::*pe1 = (T C::*) ((size_t) pma + sizeof T);

The assumption is that the pointer-to-member is an integer-like offset
that can be converted to a size_t, subject to displacement by a
multiple of the array size and the converted back to pointer-to-member.


Try for example:
struct T{
char a[16];
};
int main()
{
char (T::*p)[16] = &T::a;
size_t p1 = *(size_t*)&p;
if(p) cout << sizeof(p)<<' '<<p1<<'\n';
}

p should return true but p1 could display 0.
If you write p = 0 explicitely then "if" wouldn't print.
It's more than a size_t.

Greetings, Bane.
Oct 11 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: franky.backeljauw | last post by:
Hello, I am wondering which of these two methods is the fastest: std::copy, which is included in the standard library, or a manually written pointer copy? Do any of you have any experience with...
4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
1
by: Bryan Parkoff | last post by:
I know how to write "Pointer to Function" inside struct or class without using static, but I have decided to add static to all functions inside struct or class because I want member functions to be...
20
by: j0mbolar | last post by:
I was reading page 720 of unix network programming, volume one, second edition. In this udp_write function he does the following: void udp_write(char *buf, <everything else omitted) struct...
9
by: removeps-generic | last post by:
I have struct X { double array; }; I want to form a pointer to the 5th element of X::array. The type of the pointer should be "double X::*" or "double* X::*" or something along those...
42
by: xdevel | last post by:
Hi, if I have: int a=100, b = 200, c = 300; int *a = {&a, &b, &c}; than say that: int **b is equal to int *a is correct????
14
by: Szabolcs Borsanyi | last post by:
Deal all, The type typedef double ***tmp_tensor3; is meant to represent a three-dimensional array. For some reasons the standard array-of-array-of-array will not work in my case. Can I...
31
by: huili80 | last post by:
Say I have two classes: class A { public: int x; }; class B {
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.