473,883 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pointer to a member of a member

Say I have two classes:

class A
{
public:
int x;
};

class B
{
public:
A a;
};

Then how do I construct a member pointer to B::a.x ? What's the syntax
for it?
Thanks!
Jun 27 '08 #1
31 2002
hu*****@gmail.c om wrote:
Say I have two classes:

class A
{
public:
int x;
};

class B
{
public:
A a;
};

Then how do I construct a member pointer to B::a.x ? What's the syntax
for it?
Why do you think you need it? Does this help:

B b;
int *ptr = &b.a.x;

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
hu*****@gmail.c om wrote:
Say I have two classes:

class A
{
public:
int x;
};

class B
{
public:
A a;
};

Then how do I construct a member pointer to B::a.x ? What's the syntax
for it?
Thanks!
Not sure what you require, but the code sample below may be of help

int A::*ptr = A::x;
A Example;
Example.*ptr = 15;

JB
Jun 27 '08 #3
n2xssvv.g02gfr1 2930 wrote:
hu*****@gmail.c om wrote:
>Say I have two classes:

class A
{
public:
int x;
};

class B
{
public:
A a;
};

Then how do I construct a member pointer to B::a.x ? What's the syntax
for it?
Thanks!

Not sure what you require, but the code sample below may be of help

int A::*ptr = A::x;
int A::*ptr = &A::x;

(without the ampersand it's not legal).
A Example;
Example.*ptr = 15;
Now do that for a 'B'... :-)

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #4
Victor Bazarov wrote:
n2xssvv.g02gfr1 2930 wrote:
>hu*****@gmail.c om wrote:
>>Say I have two classes:

class A
{
public:
int x;
};

class B
{
public:
A a;
};

Then how do I construct a member pointer to B::a.x ? What's the syntax
for it?
Thanks!

Not sure what you require, but the code sample below may be of help

int A::*ptr = A::x;

int A::*ptr = &A::x;

(without the ampersand it's not legal).
>A Example;
Example.*ptr = 15;

Now do that for a 'B'... :-)

V
A careless mistake, cheers Victor

JB
Jun 27 '08 #5
On Jun 27, 10:42*am, Victor Bazarov <v.Abaza...@com Acast.netwrote:
huil...@gmail.c om wrote:
Say I have two classes:
class A
{
public:
* * int x;
};
class B
{
public:
* * A a;
};
Then how do I construct a member pointer to B::a.x ? What's the syntax
for it?

Why do you think you need it? *Does this help:

* * *B b;
* * *int *ptr = &b.a.x;
The question seems to me to be asking for a member pointer - not a
pointer to a (data) member. If that is the case, then the answer would
be that it is not possible to create a single, member pointer to
b.a.x. Instead it is necessary to declare two member pointers (one for
B::a and the other for A::x) and then apply them both. For example:

struct A
{
int x;
};

struct B
{
A a;
};

int main()
{
B b;
A B::*pa = &B::a;
int A::*pi = &A::x;

b.*pa.*pi = 3; // assigns 3 to b.a.x
}

Greg

Jun 27 '08 #6
Greg Herlihy wrote:
On Jun 27, 10:42 am, Victor Bazarov <v.Abaza...@com Acast.netwrote:
>huil...@gmail. com wrote:
>>Say I have two classes:
class A
{
public:
int x;
};
class B
{
public:
A a;
};
Then how do I construct a member pointer to B::a.x ? What's the syntax
for it?
Why do you think you need it? Does this help:

B b;
int *ptr = &b.a.x;

The question seems to me to be asking for a member pointer - not a
pointer to a (data) member. If that is the case, then the answer would
be that it is not possible to create a single, member pointer to
b.a.x. Instead it is necessary to declare two member pointers (one for
B::a and the other for A::x) and then apply them both. For example:

struct A
{
int x;
};

struct B
{
A a;
};

int main()
{
B b;
A B::*pa = &B::a;
int A::*pi = &A::x;

b.*pa.*pi = 3; // assigns 3 to b.a.x
}

Greg
I would like to see what the OP has to say about his/her need to create
such a construct.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #7
On Jun 27, 3:00*pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
Greg Herlihy wrote:
On Jun 27, 10:42 am, Victor Bazarov <v.Abaza...@com Acast.netwrote:
huil...@gmail.c om wrote:
Say I have two classes:
class A
{
public:
* * int x;
};
class B
{
public:
* * A a;
};
Then how do I construct a member pointer to B::a.x ? What's the syntax
for it?
Why do you think you need it? *Does this help:
* * *B b;
* * *int *ptr = &b.a.x;
The question seems to me to be asking for a member pointer - not a
pointer to a (data) member. If that is the case, then the answer would
be that it is not possible to create a single, member pointer to
b.a.x. Instead it is necessary to declare two member pointers (one for
B::a and the other for A::x) and then apply them both. For example:
* * struct A
* * {
* * * * int x;
* * };
* * struct B
* * {
* * * * A a;
* * };
* * int main()
* * {
* * * * B * b;
* * * * A * B::*pa = &B::a;
* * * * int A::*pi = &A::x;
* * * * b.*pa.*pi = 3; // assigns 3 to b.a.x
* * }
Greg

I would like to see what the OP has to say about his/her need to create
such a construct.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Here is an example (probably over-simplified from the actual case I'm
working on). Say I have a 2D vector class:

struct vector2d
{
double x,y;
static double vector2d::* const _v[2];
double& operator[] (int i) { return this->*_v[i]; }
const double& operator[] (int i) const { return this->*_v[i]; }
};
double vector2d::* const vector2d::_v[] = { &vector2d::x ,
&vector2d::y };

and suppose we have an object "vector2d v;" . The purpose of using
pointer to member here is to make v[0] and v.x have exactly the same
run-time efficiency, provided that the compiler is capable of
necessary optimization. (I didn't invent this technique, but I forgot
where I learned it).

Suppose now for some reason, I want to build a 5D vector class out of
this 2D vector class, say like this.

class vector5d
{
vector2d v1, v2;
double z;
};

and we have an object "vector5d w;"

What I want is, with as little run-time overhead as possible (maybe
using a similar method that's used by vector2d), that w[0] gives me
w.v1.x , w[1] gives w.v1.y , w[2] gives w.v2.x , w[3] gives w.v2.y ,
and w[4] gives me w.z .

Is it possible? If yes, how?

Thanks!
Jun 27 '08 #8
On Jun 27, 3:33*pm, huil...@gmail.c om wrote:
On Jun 27, 3:00*pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
Greg Herlihy wrote:
On Jun 27, 10:42 am, Victor Bazarov <v.Abaza...@com Acast.netwrote:
>huil...@gmail. com wrote:
>>Say I have two classes:
>>class A
>>{
>>public:
>>* * int x;
>>};
>>class B
>>{
>>public:
>>* * A a;
>>};
>>Then how do I construct a member pointer to B::a.x ? What's the syntax
>>for it?
>Why do you think you need it? *Does this help:
>* * *B b;
>* * *int *ptr = &b.a.x;
The question seems to me to be asking for a member pointer - not a
pointer to a (data) member. If that is the case, then the answer would
be that it is not possible to create a single, member pointer to
b.a.x. Instead it is necessary to declare two member pointers (one for
B::a and the other for A::x) and then apply them both. For example:
* * struct A
* * {
* * * * int x;
* * };
* * struct B
* * {
* * * * A a;
* * };
* * int main()
* * {
* * * * B * b;
* * * * A * B::*pa = &B::a;
* * * * int A::*pi = &A::x;
* * * * b.*pa.*pi = 3; // assigns 3 to b.a.x
* * }
Greg
I would like to see what the OP has to say about his/her need to create
such a construct.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Here is an example (probably over-simplified from the actual case I'm
working on). Say I have a 2D vector class:

struct vector2d
{
* * double x,y;
* * static double vector2d::* const _v[2];
* * double& operator[] (int i) { return this->*_v[i]; }
* * const double& operator[] (int i) const { return this->*_v[i]; }};

double vector2d::* const vector2d::_v[] = { &vector2d::x ,
&vector2d::y };

and suppose we have an object "vector2d v;" . The purpose of using
pointer to member here is to make v[0] and v.x have exactly the same
run-time efficiency, provided that the compiler is capable of
necessary optimization. (I didn't invent this technique, but I forgot
where I learned it).

Suppose now for some reason, I want to build a 5D vector class out of
this 2D vector class, say like this.

class vector5d
{
* * vector2d v1, v2;
* * double z;

};

and we have an object "vector5d w;"

What I want is, with as little run-time overhead as possible (maybe
using a similar method that's used by vector2d), that w[0] gives me
w.v1.x , w[1] gives w.v1.y , w[2] gives w.v2.x , w[3] gives w.v2.y ,
and w[4] gives me w.z .

Is it possible? If yes, how?
I mean, is it possible to achieve zero run-time overhead (assuming
proper optimization) in accessing members (and their members) via an
index? If we don't have a vector5d::z (in which case it's actually a
4D vector), we might want to use an array of pointers to member of a
member (I don't know how even if they do exist). Having vector5d::z
makes this even more complicated in that a pointer to vector5d::z and
a (may or may not existing) pointer to vector5d::v1.x certainly would
have different types, so they cannot be put into an array.
>
Thanks!
Jun 27 '08 #9
hu*****@gmail.c om wrote:
On Jun 27, 3:33 pm, huil...@gmail.c om wrote:
>On Jun 27, 3:00 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
>>Greg Herlihy wrote:
On Jun 27, 10:42 am, Victor Bazarov <v.Abaza...@com Acast.netwrote:
huil...@gma il.com wrote:
>Say I have two classes:
>class A
>{
>public:
> int x;
>};
>class B
>{
>public:
> A a;
>};
>Then how do I construct a member pointer to B::a.x ? What's the syntax
>for it?
Why do you think you need it? Does this help:
B b;
int *ptr = &b.a.x;
The question seems to me to be asking for a member pointer - not a
pointer to a (data) member. If that is the case, then the answer would
be that it is not possible to create a single, member pointer to
b.a.x. Instead it is necessary to declare two member pointers (one for
B::a and the other for A::x) and then apply them both. For example:
struct A
{
int x;
};
struct B
{
A a;
};
int main()
{
B b;
A B::*pa = &B::a;
int A::*pi = &A::x;
b.*pa.*pi = 3; // assigns 3 to b.a.x
}
Greg
I would like to see what the OP has to say about his/her need to create
such a construct.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Here is an example (probably over-simplified from the actual case I'm
working on). Say I have a 2D vector class:

struct vector2d
{
double x,y;
static double vector2d::* const _v[2];
double& operator[] (int i) { return this->*_v[i]; }
const double& operator[] (int i) const { return this->*_v[i]; }};

double vector2d::* const vector2d::_v[] = { &vector2d::x ,
&vector2d::y };

and suppose we have an object "vector2d v;" . The purpose of using
pointer to member here is to make v[0] and v.x have exactly the same
run-time efficiency, provided that the compiler is capable of
necessary optimization. (I didn't invent this technique, but I forgot
where I learned it).

Suppose now for some reason, I want to build a 5D vector class out of
this 2D vector class, say like this.

class vector5d
{
vector2d v1, v2;
double z;

};

and we have an object "vector5d w;"

What I want is, with as little run-time overhead as possible (maybe
using a similar method that's used by vector2d), that w[0] gives me
w.v1.x , w[1] gives w.v1.y , w[2] gives w.v2.x , w[3] gives w.v2.y ,
and w[4] gives me w.z .

Is it possible? If yes, how?
I mean, is it possible to achieve zero run-time overhead (assuming
proper optimization) in accessing members (and their members) via an
index? If we don't have a vector5d::z (in which case it's actually a
4D vector), we might want to use an array of pointers to member of a
member (I don't know how even if they do exist). Having vector5d::z
makes this even more complicated in that a pointer to vector5d::z and
a (may or may not existing) pointer to vector5d::v1.x certainly would
have different types, so they cannot be put into an array.
>Thanks!
What you seem to be looking for is

struct vector5d
{
vector2d v1, v2;
double z;
double& operator[](int i) {
switch (i) {
case 0: return v1[0];
case 1: return v1[1];
case 2: return v2[0];
case 3: return v2[1];
case 4: return z;
default: throw "bad index";
}
}
};

Isn't it?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #10

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

Similar topics

5
9386
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1, 2, func);
2
2095
by: glen stark | last post by:
Okay, I know that a function pointer and member function pointer have different types. Is it kosher to convert a member function pointer to a regurlar function pointer (with the same parameters)? I have a measurement class, and I'd like to be rather general, so store function pointers for get operations. Can I typecast a member fuction pointer and pass it to the class? I know the FAQ disucusses this a bit, but I can't infer the...
37
5042
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined signature. When developing this lib, I figured that the pointer-to-member-function, although seemingly an attractive solution, does not work well for us.
3
1371
by: jjleto | last post by:
I have just learn about "pointer to member". Does someone have an actual example where "pointers to member" are usefull or necessary ? Can't always &(myClass.member) be used ? Thank you.
1
380
by: mk | last post by:
In C++ one can get an absolute address of data member, having a pointer-to-member and a pointer to class object: struct A { int m; }; void f(A *a) {
7
3047
by: jon wayne | last post by:
Hi I'm a little confused here about the lifetime of a static pointer to member function, Say, I declare,define & initialize a static ptr to mem function in the header file of a class(the class is a helper Singleton class, and is created & deleted as and when required) - Where does the static pointer point to when every single instance of the class is deleted. I presume it'll be dangling pointer- as the code seg to which it's
12
1253
by: Vladimir_petter | last post by:
Dear All, There is the problem in nutshells (see the program bellow): It is ok to convert pointer to F<T> to the pointer to I. Now if I have pointer to member "F<T> entity::*" can I convert it to the "I entity::*"? Compiler does not let me to do that (I've tried on VC 7.1 and Comeau 4.3.3). If I do reinterpret_cast then the program will be compiled and runs as expected all though I would like to know possible implications of this
40
3062
by: Steve Rencontre | last post by:
I can't for the life of me see how to do pointer-to-member when the member is actually part of an embedded structure. That is, if I have: struct S1 { int a; }; struct S2 { S1 s; int b; }; how can I get a pointer to the a in an S2?
6
1494
by: cbull | last post by:
class A { }; class B: public A { void f(); }; typedef void (A::*MPA)(void); MPA mpA;
1
3983
by: autumn | last post by:
Hi everybody, I'm having problem passing pointer to member object as template argument, seems VC 2005 does not allow 'pointer to base member' to 'pointer to derived member' conversion in template arguments, is this VC specific or a standard c++ behavior? the code looks like this: class Base { public: int member; };
0
9942
marktang
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...
0
9792
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,...
0
11142
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, 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...
0
10745
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10848
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,...
0
9575
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7130
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5798
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3234
bsmnconsultancy
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...

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.