473,406 Members | 2,336 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,406 software developers and data experts.

member variable ptr cast

Consider the following hierarchy:

struct record
{
};

struct test_record
{
};

struct an_object
{
test_record r;
};

I want a member pointer variable to r but I want it to be cast as a
record. Is this possible? I tried the following:

static_cast<record an_object::*>(&an_object::r);

No worky.
Oct 21 '08 #1
7 1838
Noah Roberts wrote:
Consider the following hierarchy:

struct record
{
};

struct test_record
{
};

struct an_object
{
test_record r;
};

I want a member pointer variable to r but I want it to be cast as a
record. Is this possible? I tried the following:

static_cast<record an_object::*>(&an_object::r);

No worky.
'an_object' does not have a member of type 'record'.

Now, answer my question: What do you think you need that for?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 21 '08 #2
Victor Bazarov wrote:
Noah Roberts wrote:
>Consider the following hierarchy:

struct record
{
};

struct test_record
{
};

struct an_object
{
test_record r;
};

I want a member pointer variable to r but I want it to be cast as a
record. Is this possible? I tried the following:

static_cast<record an_object::*>(&an_object::r);

No worky.

'an_object' does not have a member of type 'record'.

Now, answer my question: What do you think you need that for?
test_record was supposed to be a sub of record. My mistake.
Oct 21 '08 #3
Consider the following hierarchy:

struct record
{
};

struct test_record : record
{
};

struct an_object
{
test_record r;
};

I want a member pointer variable to r but I want it to be cast as a
record. Is this possible? I tried the following:

static_cast<record an_object::*>(&an_object::r);

No worky.
Oct 21 '08 #4
Noah Roberts wrote:
Consider the following hierarchy:

struct record
{
};

struct test_record : record
{
};

struct an_object
{
test_record r;
};

I want a member pointer variable to r but I want it to be cast as a
record. Is this possible? I tried the following:

static_cast<record an_object::*>(&an_object::r);

No worky.
I still think it's not possible. An instance of type 'an_object' does
not *directly* contain a member of type 'record'. The fact that
'test_record' contains a subobject of type 'record' does not matter.
It's about the same as

struct A { int a; };

struct hasA { A myA; };

int hasA::*p = hasA::myA::a; // or some such syntax.

The use for member pointers is limited and the capabilities are limited
because the language designers weren't concerned with any possible use
of those.

You still haven't answered my question: why do you think you need that?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 21 '08 #5
Victor Bazarov wrote:
>
I still think it's not possible.
Well, I can't think of a way either.
>
You still haven't answered my question: why do you think you need that?
You're being kinda demanding, don't you think? Not exactly necessary
information to answer the question asked. I think I need it because
I've yet to come up with an alternative solution to the problem I'm
facing of course.
Oct 21 '08 #6
Noah Roberts wrote:
Victor Bazarov wrote:
>>
I still think it's not possible.

Well, I can't think of a way either.
>>
You still haven't answered my question: why do you think you need that?

You're being kinda demanding, don't you think? Not exactly necessary
information to answer the question asked. I think I need it because
I've yet to come up with an alternative solution to the problem I'm
facing of course.
Hell, if you don't need help, maybe you should just go solve your
problems by yourself, don't you think? I asked because I think that
while you're explaining you might have a revelation or somethin' or
maybe somebody here will give you an alternative to your alternative. A
pointer to member is a rarely needed thing. Have you tried a regular
pointer instead?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 22 '08 #7
Victor Bazarov wrote:
Noah Roberts wrote:
>Victor Bazarov wrote:
>>>
I still think it's not possible.

Well, I can't think of a way either.
>>>
You still haven't answered my question: why do you think you need that?

You're being kinda demanding, don't you think? Not exactly necessary
information to answer the question asked. I think I need it because
I've yet to come up with an alternative solution to the problem I'm
facing of course.

Hell, if you don't need help, maybe you should just go solve your
problems by yourself, don't you think?
I asked a pretty direct question about what I needed to know. You did
the best you could with that question. Thank you. That's all I want to
know...don't need help doing my job.

I asked because I think that
while you're explaining you might have a revelation or somethin' or
maybe somebody here will give you an alternative to your alternative. A
pointer to member is a rarely needed thing. Have you tried a regular
pointer instead?
Obviously there are some pretty important differences between member
pointer and pointer such that they are not at all interchangeable concepts.
Oct 22 '08 #8

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

Similar topics

12
by: Me | last post by:
Hi, I would like learn from people with experience in C++, which of the following styles of way to construct "get/set" member functions would be the best in terms of usability, speed, et cetera. ...
4
by: Bren | last post by:
Hi all, Given this situation: class Base { typedef void (Base::*FN_FOO)(); virtual void Foo() = 0; // pure virtual void GetFoo(FN_FOO pfnFoo) = 0; // pure virtual
12
by: MacFly | last post by:
Hi everyone, HRESULT WINAPI DirectPlayMessageHandler( PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer) I want that method to be class member method so it could have access to class...
19
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
1
by: none | last post by:
Hi, I have a base class with a pointer-to-member function variable. Then I have a derived class that needs to use that variable to call a member function (with the same arguments and return value...
3
by: Fett | last post by:
Problem: My member variables in the child class are always null. I don't know why this happens! I have a base class for handling a user flow in my web, CWebUseCase, the class is declared like:...
8
by: wkaras | last post by:
In my compiler, the following code generates an error: union U { int i; double d; }; U u; int *ip = &u.i; U *up = static_cast<U *>(ip); // error I have to change the cast to...
31
by: dragoncoder | last post by:
Consider the code class A { private: int a; }; int main(void) { A x; int* ptr = (int*)&x;
4
by: RSH | last post by:
Hi, I have a situation where I have a class which manages different data connections. In trying to enforce encapsulation I have a member field called connection which could be a type of...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
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...
0
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,...

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.