Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pointers to member functions with move semantics

When you have a pointer to a non-static member function of an object on the stack, what exactly happens when the object changes its location because of an move? Does the pointer still point to the location of the moved-from object and thus is invalid/dangling?

like image 875
Eric Avatar asked Nov 30 '25 03:11

Eric


1 Answers

Pointers to member functions do not hold a reference to any instance of the class. This is true even for pointers to non-static member functions.

Such a pointer can be constructed without having an instance of the class:

class star {
public:
    void shine();
};

void (star::* smile)() = &sun::shine;

And an instance needs to be specified when calling it:

star sun;

(sun.*smile)();
std::memfn(smile)(sun);
like image 196
Etienne Laurin Avatar answered Dec 02 '25 19:12

Etienne Laurin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!