top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

c++: What is pointer to member? Can someone please explain with an example?

+2 votes
405 views
c++: What is pointer to member? Can someone please explain with an example?
posted Dec 20, 2014 by Amit Kumar Pandey

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

In C++ programming, there are concepts of member function and variable. When a pointer points to an object and calls its member function it is known as pointer to member function. Its prototype is different from the ordinary function prototype. For example:

include

using std::cout;
void fun1(int x)
{
int y = x;
cout << y;
}

typedef class
{
int a;
public:
int fun1(int x)
{ a = x; cout << a;}

}X;
int main()
{
fun1(10); // first call
X x;
x.fun1(10); // Second call
return 0;
}

Conclusion: first call of function "fun1" is pointer to function and second call of "fun1" is pointer to member function of class X;

answer Dec 21, 2014 by Neeraj Mishra
...