[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

member variable ptr cast

john

10/21/2008 6:43:00 PM

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.
7 Answers

Victor Bazarov

10/21/2008 7:04:00 PM

0

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

john

10/21/2008 7:46:00 PM

0

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.

john

10/21/2008 7:47:00 PM

0

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.

Victor Bazarov

10/21/2008 8:47:00 PM

0

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

john

10/21/2008 11:30:00 PM

0

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.

Victor Bazarov

10/22/2008 1:25:00 AM

0

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

john

10/22/2008 11:12:00 PM

0

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.