 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
cbull Guest
|
Posted: Fri Feb 24, 2006 3:06 am Post subject: 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
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
++Hector C Guest
|
Posted: Fri Feb 24, 2006 10:06 am Post subject: Re: Casting pointer to member. |
|
|
I can see two reasons why it doesn't work.
1. Precedence: Function call () has higher precedence than pointer to
member object and pointer .* and ->*. You should use parenthesis.
2. The pointer to member pointer requires a pointer. You should use
pointer to member object instead.
The following code compiles (although you destroyed type safety with
the casting)
class A {};
class B : public A
{
public:
void f(){}
};
typedef void ( A::* MPA )( void );
int main()
{
MPA mpA;
B b;
mpA = ( MPA ) & B::f;
(b.*mpA)();
return 0;
}
++Hector C
cbull wrote:
| Quote: | 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
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
kanze Guest
|
Posted: Fri Feb 24, 2006 11:06 am Post subject: Re: Casting pointer to member. |
|
|
cbull wrote:
| Quote: | 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?
|
It shouldn't even compile. The left operand of ->* must be a
pointer (barring user defined overloading), and b is not a
pointer.
There's also a problem of precedence.
| Quote: | If not then when it will fail? I've heard something about
multi inheritance.
|
If you replace b->*mpA() by (b.*mpA)(), the standard guarantees
that it will work. If you have a pointer p of type A*, and the
actual object type is B, (p->*mpA)() is also guaranteed to work.
I wouldn't be too surprised if some compilers got it wrong,
however. It isn't exactly the most used feature of C++.
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Bob Hairgrove Guest
|
Posted: Fri Feb 24, 2006 11:06 am Post subject: Re: Casting pointer to member. |
|
|
On 23 Feb 2006 21:28:50 -0500, "cbull" <cbull (AT) poczta (DOT) onet.pl> wrote:
| Quote: | class A
{
};
class B: public A
{
void f();
};
typedef void (A::*MPA)(void);
MPA mpA;
B b;
mpA = (MPA) &B::f;
b->*mpA();
|
You need parentheses here:
(b->*mpA)();
| Quote: | Will it work? If not then when it will fail? I've heard something about
multi inheritance.
Tomasz
|
As long as the dynamic type of the object used to dereference the
pointer is B, it should work. Otherwise, the behavior is undefined
(see section 5.2.9, paragraph 9 of the C++ standard). You should use
static_cast instead of a C-style cast, though.
--
Bob Hairgrove
NoSpamPlease (AT) Home (DOT) com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Bob Hairgrove Guest
|
Posted: Fri Feb 24, 2006 3:06 pm Post subject: Re: Casting pointer to member. |
|
|
On 24 Feb 2006 05:39:15 -0500, Bob Hairgrove <invalid (AT) bigfoot (DOT) com>
wrote:
Oops ... surely you meant:
B *b;
Of course, then you would need to initialize the pointer to some valid
object ... so maybe you didn't mean that?
| Quote: | mpA = (MPA) &B::f;
b->*mpA();
You need parentheses here:
(b->*mpA)();
|
Only if b is a B* and not a B will that work.
--
Bob Hairgrove
NoSpamPlease (AT) Home (DOT) com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Kodt Guest
|
Posted: Fri Feb 24, 2006 3:06 pm Post subject: Re: Casting pointer to member. |
|
|
cbull wrote:
| Quote: | 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.
|
Syntax errors:
- using ->* instead of .*
- precedence of ()
should be fixed as follows
(b.*mpA)();
A pointer-to-member-of-T should be appliable to _any_ object of class T
(or its descendants).
It doesn't matter what compilers do in background (taking offsets and
virtualness into account): just imagine you substitute your value
manually.
A member function of B cannot be applied to an instance of A. Thus,
(MPA) &B::f is at least meaningless.
But downcasting (from member of A to pointer-to-member-of-B) is
reasonable, as you can invoke inherited functions.
In fact, your compiler does here a reinterpret_cast<MPA*> and, luckily,
MPA and MPB have same layout (just pointers to code) and same
background processing (nothing) - that's why your program work.
We can invent a case when it fail to work. For instance,
struct A {}; // no virtual functions
typedef void (A::*MPA)(void);
struct B : A { virtual ~B(); }; // let's introduce virtual functions
typedef void (B::*MPB)(void); // sizeof(MPB) > sizeof(MPA);
struct A2 {};
struct C : A, A2 {}; // multiple inheritance
typedef void (C::*MPC)(void); // sizeof(MPC) > sizeof(MPA);
So, conversion MPB->MPA is impossible without loss of data.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|