Channels ▼

Walter Bright

Dr. Dobb's Bloggers

Member Function Pointers in D

August 31, 2011

C++ and D share the ability to take the address of a function, pass it around, and call that function. It's called a function pointer, which we all know and love. That works fine for regular functions, but what about member functions of a class? Calling a member function needs a so-called this pointer — a reference to the object upon which the member function acts.

C++ and D diverge here. D has a delegate (aka "fat pointer"), which is a pair consisting of a pointer to the member function and a pointer to the this object. The virtualness of the member function was resolved at the point where the address of the member function was taken,

class C {
    int a;
   int foo(int i) { return i + a; }
}

auto c = new C();
auto dg = &c.foo;
dg(1);

because the function and its object are bound together at that point, rather than at the call point.

Alternatively, C++ has a member function pointer, which is independent of whatever object is used to call it. Such an object is provided when the member function is called:

class C {
  public:
   int a;
   virtual int foo(int i) { return i + a; }
};

auto mfp = &C::foo;
auto c = new C();
(c->*mfp)(1);

If C::foo() is a virtual function, then which function to call is not resolved at mfp assignment time, but at mfp call time when the actual object is supplied.(See Bartosz Milewski's C++ In Action for a nice use case of C++ member function pointers.)

Can this pattern be duplicated in D?

Fortunately, there is an easy way [1]:

class C {
   int a;
   int foo(int i) { return i + a; }
}

auto mfp = function(C self, int i) { return self.foo(i); };
auto c = new C();
mfp(c, 1);

It is line for line analogous to the C++ example. Pretty much the only interesting bit is the use of an anonymous lambda function to lazily bind the object with the function and resolve any virtualness. Those familiar with the guts of how C++ compilers implement member function pointers [2] will recognize this lambda function as what essentially happens under the C++ hood.

Conclusion

Lambda functions are incredibly powerful, and for those not used to them, they continue to surprise and delight in what they can do in a simple, straightforward manner.

References

  1. Timon Gehr's example

  2. Member Function Pointers and the Fastest Possible C++ Delegates
    by Don Clugston

Thanks to Jason House and Bartosz Milewski for their helpful comments on this post.

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Video