472,992 Members | 3,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,992 software developers and data experts.

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 1440
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ghostdog | last post by:
hi, i got this opengl/c++ code: <code> void render(CMesh *mesh){ ... float *pVertices; int *pIndices;
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
4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
20
by: j0mbolar | last post by:
I was reading page 720 of unix network programming, volume one, second edition. In this udp_write function he does the following: void udp_write(char *buf, <everything else omitted) struct...
44
by: Agoston Bejo | last post by:
What happens exactly when I do the following: struct A { int i; string j; A() {} }; void f(A& a) { cout << a.i << endl;
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;
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
4
by: Wally Barnes | last post by:
Can someone help a poor C++ programmer that learned the language before there was a standard lib .. etc ? Basically I have two classes that look something like below: template <class T>...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.