54

In C++, can member function pointers be used to point to derived (or even base) class members?

EDIT: Perhaps an example will help. Suppose we have a hierarchy of three classes X, Y, Z in order of inheritance. Y therefore has a base class X and a derived class Z.

Now we can define a member function pointer p for class Y. This is written as:

void (Y::*p)();

(For simplicity, I'll assume we're only interested in functions with the signature void f() )

This pointer p can now be used to point to member functions of class Y.

This question (two questions, really) is then:

  1. Can p be used to point to a function in the derived class Z?
  2. Can p be used to point to a function in the base class X?
smh
  • 1,233
  • 1
  • 12
  • 16
  • Why would you want to do this? – Landon Sep 12 '08 at 21:44
  • I agree with Matt. Your question needs clarification. Are you simply wanting to know if member function pointers work with virtual functions? If so, then the answer is yes. If you are trying to ask something else, please clarify. – Derek Park Sep 12 '08 at 21:57
  • 10
    I think it's clear as asked. +1'ed, and smh: Do you need further explanation? Please point out whether that's so. – Johannes Schaub - litb Oct 25 '09 at 23:22

8 Answers8

32

C++03 std, §4.11 2 Pointer to member conversions:

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. If B is an inaccessible (clause 11), ambiguous (10.2) or virtual (10.1) base class of D, a program that necessitates this conversion is ill-formed. The result of the conversion refers to the same member as the pointer to member before the conversion took place, but it refers to the base class member as if it were a member of the derived class. The result refers to the member in D’s instance of B. Since the result has type “pointer to member of D of type cv T,” it can be dereferenced with a D object. The result is the same as if the pointer to member of B were dereferenced with the B sub-object of D. The null member pointer value is converted to the null member pointer value of the destination type. 52)

52)The rule for conversion of pointers to members (from pointer to member of base to pointer to member of derived) appears inverted compared to the rule for pointers to objects (from pointer to derived to pointer to base) (4.10, clause 10). This inversion is necessary to ensure type safety. Note that a pointer to member is not a pointer to object or a pointer to function and the rules for conversions of such pointers do not apply to pointers to members. In particular, a pointer to member cannot be converted to a void*.

In short, you can convert a pointer to a member of an accessible, non-virtual base class to a pointer to a member of a derived class as long as the member isn't ambiguous.

class A {
public: 
    void foo();
};
class B : public A {};
class C {
public:
    void bar();
};
class D {
public:
    void baz();
};
class E : public A, public B, private C, public virtual D {
public: 
    typedef void (E::*member)();
};
class F:public E {
public:
    void bam();
};
...
int main() {
   E::member mbr;
   mbr = &A::foo; // invalid: ambiguous; E's A or B's A?
   mbr = &C::bar; // invalid: C is private 
   mbr = &D::baz; // invalid: D is virtual
   mbr = &F::bam; // invalid: conversion isn't defined by the standard
   ...

Conversion in the other direction (via static_cast) is governed by § 5.2.9 9:

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 class.derived) 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 conv.mem), and cv2 is the same cv-qualification as, or greater cv-qualification than, cv1.11) The null member pointer value (4.11 conv.mem) is converted to the null member pointer value of the destination type. If class B contains the original member, or is a base or derived class of the class containing the original member, the resulting pointer to member points to the original member. Otherwise, the result of the cast is undefined. [Note: although class B need not contain the original member, the dynamic type of the object on which the pointer to member is dereferenced must contain the original member; see 5.5 expr.mptr.oper.]

11) Function types (including those used in pointer to member function types) are never cv-qualified; see 8.3.5 dcl.fct.

In short, you can convert from a derived D::* to a base B::* if you can convert from a B::* to a D::*, though you can only use the B::* on objects that are of type D or are descended from D.

outis
  • 75,655
  • 22
  • 151
  • 221
  • +1. Over at http://stackoverflow.com/questions/4295117/pointer-to-member-conversion they find that 5.2.9/9 says that `static_cast` can convert a `T D::*` to a `T B::*`, though of course this requires that you either (a) know not to call nonexistent methods through it; or (b) know that the `B` object's dynamic type is actually `D` or something derived from it. – j_random_hacker Nov 28 '10 at 13:35
  • 4
    Thanks. Updated answer. [Is it safe to “upcast” a method pointer and use it with base class pointer?](http://stackoverflow.com/questions/4272909/is-it-safe-to-upcast-a-method-pointer-and-use-it-with-base-class-pointer/) is also relevant. – outis Nov 29 '10 at 01:43
  • "In short, you can convert from a derived `D::*` to a base `B::*` if you can convert from a `B::*` to a `D::*`, though you can only use the `B::*` on objects that are of type `D` or are descended from `D`." - false. If member pointed to by `D::*` is not contained in `B` (or it's base classes), then already casting the pointer to `B::*` is producing undefined behavior. – j_kubik Jul 09 '13 at 07:04
  • @j_kubik: not according to the standard; as stated in the quote, "If class B [...] is a base [...] class of the class containing the original member, the resulting pointer to member points to the original member." This surprised me, too, when I first read it. Also: "Note: although class B need not contain the original member, the dynamic type of the object on which the pointer to member is dereferenced must contain the original member." My final line still holds. – outis Jul 09 '13 at 21:10
  • Ok, what if i.e. `B` has no virtual methods, but `D` has? Then pointer to `B`'s method for some compilers will have different size (usually smaller) than to `D`'s. Since those additional bytes are added for some purpose (why?), cut-down pointer to `B::*` will be unable to correctly represent `D`'s method, even if dynamic type of object is `D`. – j_kubik Jul 10 '13 at 06:20
  • Ok, now I noticed that such casting (with some limitations that standard introduces) can actually be accomplished without adding anything in existing implementations. Quite surprising anyway, and questionably useful I think. – j_kubik Jul 10 '13 at 08:30
  • @j_kubik: a-hyup. I just figure the standards committee knows more than us and had their reasons. Since I'm not implementing a compiler, it's easy to accept. Who know? If I were, maybe I'd complain more. – outis Jul 10 '13 at 18:47
  • I am also not writing a compiler, but knowing one or two example implementations of C/C++ language constructs might be a good idea anyway. – j_kubik Jul 11 '13 at 06:56
12

I'm not 100% sure what you are asking, but here is an example that works with virtual functions:

#include <iostream>
using namespace std;

class A { 
public:
    virtual void foo() { cout << "A::foo\n"; }
};
class B : public A {
public:
    virtual void foo() { cout << "B::foo\n"; }
};

int main()
{
    void (A::*bar)() = &A::foo;
    (A().*bar)();
    (B().*bar)();
    return 0;
}
Matt Price
  • 43,887
  • 9
  • 38
  • 44
7

The critical issue with pointers to members is that they can be applied to any reference or pointer to a class of the correct type. This means that because Z is derived from Y a pointer (or reference) of type pointer (or reference) to Y may actually point (or refer) to the base class sub-object of Z or any other class derived from Y.

void (Y::*p)() = &Z::z_fn; // illegal

This means that anything assigned to a pointer to member of Y must actually work with any Y. If it was allowed to point to a member of Z (that wasn't a member of Y) then it would be possible to call a member function of Z on some thing that wasn't actually a Z.

On the other hand, any pointer to member of Y also points the member of Z (inheritance means that Z has all the attributes and methods of its base) is it is legal to convert a pointer to member of Y to a pointer to member of Z. This is inherently safe.

void (Y::*p)() = &Y::y_fn;
void (Z::*q)() = p; // legal and safe
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
3

You might want to check out this article Member Function Pointers and the Fastest Possible C++ Delegates The short answer seems to be yes, in some cases.

dagorym
  • 5,695
  • 3
  • 25
  • 23
1

My experimentation revealed the following: Warning - this might be undefined behaviour. It would be helpful if someone could provide a definitive reference.

  1. This worked, but required a cast when assigning the derived member function to p.
  2. This also worked, but required extra casts when dereferencing p.

If we're feeling really ambitious we could ask if p can be used to point to member functions of unrelated classes. I didn't try it, but the FastDelegate page linked in dagorym's answer suggests it's possible.

In conclusion, I'll try to avoid using member function pointers in this way. Passages like the following don't inspire confidence:

Casting between member function pointers is an extremely murky area. During the standardization of C++, there was a lot of discussion about whether you should be able to cast a member function pointer from one class to a member function pointer of a base or derived class, and whether you could cast between unrelated classes. By the time the standards committee made up their mind, different compiler vendors had already made implementation decisions which had locked them into different answers to these questions. [FastDelegate article]

smh
  • 1,233
  • 1
  • 12
  • 16
1

Assume that we have class X, class Y : public X, and class Z : public Y

You should be able to assign methods for both X, Y to pointers of type void (Y::*p)() but not methods for Z. To see why consider the following:

void (Y::*p)() = &Z::func; // we pretend this is legal
Y * y = new Y; // clearly legal
(y->*p)(); // okay, follows the rules, but what would this mean?

By allowing that assignment we permit the invocation of a method for Z on a Y object which could lead to who knows what. You can make it all work by casting the pointers but that is not safe or guaranteed to work.

Winston Ewert
  • 44,070
  • 10
  • 68
  • 83
1

I believe so. Since the function pointer uses the signature to identify itself, the base/derived behavior would rely on whatever object you called it on.

Steve Duitsman
  • 2,749
  • 5
  • 27
  • 39
0

Here is an example of what works. You can override a method in derived class, and another method of base class that uses pointer to this overridden method indeed calls the derived class's method.

#include <iostream>
#include <string>

using namespace std;

class A {
public:
    virtual void traverse(string arg) {
        find(&A::visit, arg);
    }

protected:
    virtual void find(void (A::*method)(string arg),  string arg) {
        (this->*method)(arg);
    }

    virtual void visit(string arg) {
        cout << "A::visit, arg:" << arg << endl;
    }
};

class B : public A {
protected:
    virtual void visit(string arg) {
        cout << "B::visit, arg:" << arg << endl;
    }
};

int main()
{
    A a;
    B b;
    a.traverse("one");
    b.traverse("two");
    return 0;
}
Gena Batsyan
  • 736
  • 5
  • 14