Question: In C++ you can also declare pointers to non-static class members. Both function and data members can be pointed to. The pointers are not really
class C
{
public:
void f() {cout
void g() {cout
};
int main()
{
C c;
// Using an object
void (C::*pmf)() = &C::f;
(c.*pmf)(); // Executes c.f()
pmf = &C::g;
(c.*pmf)(); // Executes c.g()
// Using pointer to an object
C* cp = &c;
pmf = &C::f;
(cp->*pmf)(); // Executes cp->f()
pmf = &C::g;
(cp->*pmf)(); // Executes cp->g()
}
Compile and run this program and observe the results. Make sure you understand it. Youll have to include , of course. Make up your own example that uses pointers to data members.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
