470,849 Members | 1,149 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

Casting pointer to member

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
Feb 23 '06 #1
6 1337
cbull wrote:
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.


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
Feb 23 '06 #2

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:hQ*****************@newsread1.mlpsca01.us.to. verio.net...
cbull wrote:
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.


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?


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
Feb 23 '06 #3
cbull wrote:
[..] Will it always work while calling on object that this method
belongs to?
It's a part of slot - signal system.


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
Feb 23 '06 #4
"cbull" <cb***@poczta.onet.pl> schrieb im Newsbeitrag
news:dt**********@atlantis.news.tpi.pl...
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.


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
Feb 23 '06 #5
Heinz Ozwirk wrote:
"cbull" <cb***@poczta.onet.pl> schrieb im Newsbeitrag
news:dt**********@atlantis.news.tpi.pl...
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.


[...] 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.


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
Feb 23 '06 #6

"cbull" <cb***@poczta.onet.pl> wrote in message
news:dt**********@atlantis.news.tpi.pl...
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


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

Tomasz
Feb 23 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by ghostdog | last post: by
4 posts views Thread by Bren | last post: by
4 posts views Thread by Jacob Jensen | last post: by
20 posts views Thread by j0mbolar | last post: by
44 posts views Thread by Agoston Bejo | last post: by
8 posts views Thread by wkaras | last post: by
3 posts views Thread by Beta What | last post: by
4 posts views Thread by Wally Barnes | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.