Connecting Tech Pros Worldwide Forums | Help | Site Map

Casting pointer to member

cbull
Guest
 
Posts: n/a
#1: Feb 23 '06
class A
{
};
class B: public A
{
void f();
};

typedef void (A::*MPA)(void);
MPA mpA;

B b;
mpA = (MPA) &B::f;

b->*mpA();


Will it work? If not then when it will fail? I've heard something about
multi inheritance.

Tomasz



Victor Bazarov
Guest
 
Posts: n/a
#2: Feb 23 '06

re: Casting pointer to member


cbull wrote:[color=blue]
> class A
> {
> };
> class B: public A
> {
> void f();
> };
>
> typedef void (A::*MPA)(void);
> MPA mpA;
>
> B b;
> mpA = (MPA) &B::f;
>
> b->*mpA();
>
>
> Will it work? If not then when it will fail? I've heard something about
> multi inheritance.[/color]

No, it won't work. B::f is not a member of A. Pretending that it is will
not help solving whatever problem you're trying to solve. BTW, what is it
that you're trying to accomplish?

V
--
Please remove capital As from my address when replying by mail
cbull
Guest
 
Posts: n/a
#3: Feb 23 '06

re: Casting pointer to member



"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:hQnLf.1418$a97.1036@newsread1.mlpsca01.us.to. verio.net...[color=blue]
> cbull wrote:[color=green]
> > class A
> > {
> > };
> > class B: public A
> > {
> > void f();
> > };
> >
> > typedef void (A::*MPA)(void);
> > MPA mpA;
> >
> > B b;
> > mpA = (MPA) &B::f;
> >
> > b->*mpA();
> >
> >
> > Will it work? If not then when it will fail? I've heard something about
> > multi inheritance.[/color]
>
> No, it won't work. B::f is not a member of A. Pretending that it is will
> not help solving whatever problem you're trying to solve. BTW, what is it
> that you're trying to accomplish?[/color]

But I'm sure that I will always call it on correct object. And that is my
question. Will it always work while calling on object that this method
belongs to?
It's a part of slot - signal system.

Tomasz


Victor Bazarov
Guest
 
Posts: n/a
#4: Feb 23 '06

re: Casting pointer to member


cbull wrote:[color=blue]
> [..] Will it always work while calling on object that this method
> belongs to?
> It's a part of slot - signal system.[/color]

Actually, I am not sure. You'd need to dig into pointer adjustments
made to the member pointer when accessing (calling) a member of the base
for the object of the derived. Generally speaking, a pointer to member
function probably doesn't change when you cast it (or even when you
convert it properly). However, internally it may end up accessing the
wrong portion of the derived object.

V
--
Please remove capital As from my address when replying by mail
Heinz Ozwirk
Guest
 
Posts: n/a
#5: Feb 23 '06

re: Casting pointer to member


"cbull" <cbull@poczta.onet.pl> schrieb im Newsbeitrag
news:dtl12g$8ov$1@atlantis.news.tpi.pl...[color=blue]
> class A
> {
> };
> class B: public A
> {
> void f();
> };
>
> typedef void (A::*MPA)(void);
> MPA mpA;
>
> B b;
> mpA = (MPA) &B::f;
>
> b->*mpA();
>
>
> Will it work? If not then when it will fail? I've heard something about
> multi inheritance.[/color]

First I thought, such things cannot work, but after reading some pages of
the standard I have my doubts

4.11 (Standard Conversions) says "An rvalue of type "pointer to member of B
of type cv T," where B is a class type, can be converted to an rvalue of
type "pointer to member of D of type cv T," where D is a derived class
(clause 10) of B."

Later 5.2.9 (static_cast) says "An rvalue of type "pointer to member of D of
type cv1 T" can be converted to an rvalue of type "pointer to member of B of
type cv2 T", where B is a base class (clause 10) of D, if a valid standard
conversion from "pointer to member of B of type T" to "pointer to member of
D of type T" exists (4.11), and cv2 is the same cv-qualification as, or
greater cv-qualification than, cv1"

Putting those pieces together I come to the conclusion that it should work,
if I correctly understand those parts of the standards and didn't miss other
important parts (for this problem). According to 4.11 there is a standard
conversion from void(A::*)() [aka MPA] to void(B::*)(), and, according to
5.2.9, if there is such a conversion, you can explicitly cast from void
(B::*)() to void (A::*)() using a static_cast.

HTH
Heinz


Victor Bazarov
Guest
 
Posts: n/a
#6: Feb 23 '06

re: Casting pointer to member


Heinz Ozwirk wrote:[color=blue]
> "cbull" <cbull@poczta.onet.pl> schrieb im Newsbeitrag
> news:dtl12g$8ov$1@atlantis.news.tpi.pl...
>[color=green]
>>class A
>>{
>>};
>>class B: public A
>>{
>> void f();
>>};
>>
>>typedef void (A::*MPA)(void);
>>MPA mpA;
>>
>>B b;
>>mpA = (MPA) &B::f;
>>
>>b->*mpA();
>>
>>
>>Will it work? If not then when it will fail? I've heard something about
>>multi inheritance.[/color]
>
> [...] I come to the conclusion that it should work,
> if I correctly understand those parts of the standards and didn't miss other
> important parts (for this problem). According to 4.11 there is a standard
> conversion from void(A::*)() [aka MPA] to void(B::*)(), and, according to
> 5.2.9, if there is such a conversion, you can explicitly cast from void
> (B::*)() to void (A::*)() using a static_cast.[/color]

One thing is to convert pointers to members that way. Using them seems to
me a whole different matter.

V
--
Please remove capital As from my address when replying by mail
cbull
Guest
 
Posts: n/a
#7: Feb 23 '06

re: Casting pointer to member



"cbull" <cbull@poczta.onet.pl> wrote in message
news:dtl12g$8ov$1@atlantis.news.tpi.pl...[color=blue]
> class A
> {
> };
> class B: public A
> {
> void f();
> };
>
> typedef void (A::*MPA)(void);
> MPA mpA;
>
> B b;
> mpA = (MPA) &B::f;
>
> b->*mpA();
>
>
> Will it work? If not then when it will fail? I've heard something about
> multi inheritance.
>
> Tomasz[/color]

I've tried this construction under VC++ 2005 and it seems to work. Later I
will try it under gcc.

Tomasz


Closed Thread