473,396 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1466
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.