www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What is the D equivalent of C++'s method pointers?

reply Tejas <notrealemail gmail.com> writes:
Given a class ```Employee``` , if I have the following code
```
int (Employee::*methodPtr) () const { &Employee::getSalary };
Employee employee { "John", "Doe" };
cout << (employee.*methodPtr)() << endl;

```

What is the equivalent D code?

Ditto for pointer to class instances:

```
int (Employee::*methodPtr) () const { &Employee::getSalary };
Employee* employee { new Employee { "John", "Doe" } };
cout << (employee->*methodPtr)() << endl;

```
Jul 08 2021
next sibling parent Tejas <notrealemail gmail.com> writes:
On Thursday, 8 July 2021 at 11:53:42 UTC, Tejas wrote:
 Given a class ```Employee``` , if I have the following code
 ```
 int (Employee::*methodPtr) () const { &Employee::getSalary };
 Employee employee { "John", "Doe" };
 cout << (employee.*methodPtr)() << endl;

 ```

 What is the equivalent D code?

 Ditto for pointer to class instances:

 ```
 int (Employee::*methodPtr) () const { &Employee::getSalary };
 Employee* employee { new Employee { "John", "Doe" } };
 cout << (employee->*methodPtr)() << endl;

 ```
All I found was this post from 2005(!): https://forum.dlang.org/thread/dfhe54$1ipt$1 digitaldaemon.com
Jul 08 2021
prev sibling parent reply Paul Backus <snarwin gmail.com> writes:
On Thursday, 8 July 2021 at 11:53:42 UTC, Tejas wrote:
 Given a class ```Employee``` , if I have the following code
 ```
 int (Employee::*methodPtr) () const { &Employee::getSalary };
 Employee employee { "John", "Doe" };
 cout << (employee.*methodPtr)() << endl;

 ```

 What is the equivalent D code?

 Ditto for pointer to class instances:

 ```
 int (Employee::*methodPtr) () const { &Employee::getSalary };
 Employee* employee { new Employee { "John", "Doe" } };
 cout << (employee->*methodPtr)() << endl;

 ```
https://digitalmars.com/articles/b68.html
Jul 08 2021
parent Tejas <notrealemail gmail.com> writes:
On Thursday, 8 July 2021 at 12:04:01 UTC, Paul Backus wrote:
 https://digitalmars.com/articles/b68.html
Thank you very much :D
Jul 08 2021