digitalmars.D.learn - Pointers to methods
- nazriel (34/34) Mar 03 2013 Greetings.
- bearophile (6/9) Mar 03 2013 I think the answer to this so common question should go here
- nazriel (8/17) Mar 03 2013 If that question was asked before then I am very sorry.
- nazriel (8/17) Mar 03 2013 Oh, I also tried all variations of
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (45/46) Mar 03 2013 Thank you very much for doing that. It is the only way to ensure that
- nazriel (3/49) Mar 03 2013 The 2nd one is what I was looking for.
Greetings. While playing with D code (http://dpaste.dzfl.pl/c6d9e5bd) I noticed that I have no idea how to write equivalent to this C++ code: http://dpaste.dzfl.pl/ae182695 *[1] Could somebody point me out how to achieve same thing? Maybe I am missing something obvious? Thanks! *[1] - code for lazy people : #include <cstdio> class A { int x; public: A(int y) : x(y){} void foo() { printf("::foo(), x: %d\n", x); } }; class B : public A { public: B(int y) : A(y) {} }; int main(void) { void (A::*fp)() = &A::foo; A a(3); B b(4); (a.*fp)(); (b.*fp)(); return 0; }
Mar 03 2013
nazriel:While playing with D code (http://dpaste.dzfl.pl/c6d9e5bd) I noticed that I have no idea how to write equivalent to this C++ code:I think the answer to this so common question should go here (unless already present): http://dlang.org/faq.html Bye, bearophile
Mar 03 2013
On Monday, 4 March 2013 at 03:28:30 UTC, bearophile wrote:nazriel:If that question was asked before then I am very sorry. Documentation doesn't mention this particular case. All it mentions are delegates: A a = new A(); auto fp = &a.func; // <- Not what I am asking about. Also if you know the answer please, feel free to tell me. It would speed up work with stuff I have to do heheWhile playing with D code (http://dpaste.dzfl.pl/c6d9e5bd) I noticed that I have no idea how to write equivalent to this C++ code:I think the answer to this so common question should go here (unless already present): http://dlang.org/faq.htmlBye, bearophile
Mar 03 2013
On Monday, 4 March 2013 at 03:28:30 UTC, bearophile wrote:nazriel:Oh, I also tried all variations of (obj.*fpp)() (obj).*fpp() (obj).(*fpp)() obj.*fpp() Of course everything fails, either with parsing errors or resolution errorsWhile playing with D code (http://dpaste.dzfl.pl/c6d9e5bd) I noticed that I have no idea how to write equivalent to this C++ code:I think the answer to this so common question should go here (unless already present): http://dlang.org/faq.htmlBye, bearophile
Mar 03 2013
On 03/03/2013 07:21 PM, nazriel wrote:*[1] - code for lazy people :Thank you very much for doing that. It is the only way to ensure that these threads will remain complete. Here are two ways depending on what you need: import std.stdio; class A { int x; public: this (int y) { x = y; } void foo() { writefln("::foo(), x: %s", x); } }; class B : A { public: this(int y) { super(y); } }; void main() { { auto a = new A(3); auto b = new B(4); // When objects are available up front, initialize the function // pointer by an object: auto fp = &a.foo; fp(); fp = &b.foo; fp(); } { // When no object is available up front, use a function literal to be // called with objects later on: auto fp = ((A o) => o.foo()); auto a = new A(5); auto b = new B(6); fp(a); fp(b); } } Ali
Mar 03 2013
On Monday, 4 March 2013 at 03:44:20 UTC, Ali Çehreli wrote:On 03/03/2013 07:21 PM, nazriel wrote:The 2nd one is what I was looking for. Thanks a lot Ali.*[1] - code for lazy people :Thank you very much for doing that. It is the only way to ensure that these threads will remain complete. Here are two ways depending on what you need: import std.stdio; class A { int x; public: this (int y) { x = y; } void foo() { writefln("::foo(), x: %s", x); } }; class B : A { public: this(int y) { super(y); } }; void main() { { auto a = new A(3); auto b = new B(4); // When objects are available up front, initialize the function // pointer by an object: auto fp = &a.foo; fp(); fp = &b.foo; fp(); } { // When no object is available up front, use a function literal to be // called with objects later on: auto fp = ((A o) => o.foo()); auto a = new A(5); auto b = new B(6); fp(a); fp(b); } }
Mar 03 2013