Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
March 04, 2013 Pointers to methods | ||||
---|---|---|---|---|
| ||||
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; } |
March 04, 2013 Re: Pointers to methods | ||||
---|---|---|---|---|
| ||||
Posted in reply to nazriel | 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 |
March 04, 2013 Re: Pointers to methods | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Monday, 4 March 2013 at 03:28:30 UTC, bearophile wrote: > 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 > 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 hehe > Bye, > bearophile |
March 04, 2013 Re: Pointers to methods | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Monday, 4 March 2013 at 03:28:30 UTC, bearophile wrote: > 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 > 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 errors > Bye, > bearophile |
March 04, 2013 Re: Pointers to methods | ||||
---|---|---|---|---|
| ||||
Posted in reply to nazriel | 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 |
March 04, 2013 Re: Pointers to methods | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Monday, 4 March 2013 at 03:44:20 UTC, Ali Çehreli wrote:
> 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);
> }
> }
>
The 2nd one is what I was looking for.
Thanks a lot Ali.
|
Copyright © 1999-2021 by the D Language Foundation