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. 3 5849
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
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
"Kaz Kylheku" <kk******@gmail .com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 this?
I would think that the library function std::copy would perform optimally,
as it is a library function, and therefore the writers of this function
would know best how to optimize it ... but some tests seem to indicate
that my pointer copy...
|
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 14882:2003(E) §8.5 says:
To zero-initialize an object of type T means:
5
-- if T is a scalar type (3.9), the object is set to the value of 0 (zero)
converted to T;
|
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 bound inside struct or
class to become global functions. It makes easier for me to use
"struct.memberfunction()" instead of "globalfunction()" when I have to use
dot between struct and member function rather than global function.
I do not have...
|
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 udpiphdr *ui;
struct ip *ip;
ip = (struct ip *) buf;
|
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 lines.
| |
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????
|
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 convert an object of this type to the following type?
|
by: huili80 |
last post by:
Say I have two classes:
class A
{
public:
int x;
};
class B
{
|
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 10 member functions. Can
switch be replaced to member function pointer array?
Please provide me an example of source code to show smart pointer
inside class. Thanks....
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |