C ++ pointer to a class member function

Source: Internet
Author: User

Recently, function pointers have been used in development, so I want to sort out the concept of function pointers. O (distinct _ distinct) O ~

First, the function pointer is a pointer to a group of functions of the same type. We can also think that a class member function is a pointer to a group of member functions of the same type, of course, the member functions here should more accurately refer to non-static member functions. The former directly points to the function address, while the latter literally knows that it is definitely related to classes and objects.

Function pointer instance:

Typedef int (* p) (INT, INT); // defines a function pointer type that accepts two int types and returns int type variables.
Int func (int x, int y)
{
Printf ("FUNC: x = % d, y = % d/N", x, y );
Return (x <Y? X: Y );
}

Int main ()
{
P fun = func; // define the function pointer and assign it a function pointer
Cout <"Min:" <(* Fun) () <Endl; // Why does * Fun need to be expanded? Because * has a lower operator priority than (), if () is not used, it becomes * (fun ())
Return 0;
}

The difference between "pointer to a class member function" is:

Class
{
Public:
Int func (int x, int y)
{
Printf ("A: FUNC: x = % d, y = % d/N", x, y );
Return (x <Y? X: Y );
}
};
Typedef int (A: * p) (INT, INT); // before the pointer name, you must add the type class name :.

Int main ()
{
P fun = & A: func;
A A; // because the address of the member function must be included with the address of an object, we must create an object.
Cout <"Min:" <(A. * Fun) (4, 5) <Endl;
Return 0;
}

Hey .. It's just getting used up. * It feels strange.

 

Next, we can expand the following parameters:

# Include <tchar. h>
# Include <iostream>
# Include <stdio. h>
Using namespace STD;

Class
{
Public:
Int func1 (int x, int y)
{
Printf ("A: FUNC: x = % d, y = % d/N", x, y );
Return (x <Y? X: Y );
}
Virtual int func2 (int x, int y)
{
Printf ("A: FUNC: x = % d, y = % d/N", x, y );
Return (x> Y? X: Y );
}
};
Class B: public
{
Public:
Virtual int func2 (int x, int y)
{
Printf ("B: FUNC: x = % d, y = % d/N", x, y );
Return (x + y );
}

};
Typedef int (A: * p) (INT, INT); // before the pointer name, you must add the type class name :.
Typedef int (B: * P0) (INT, INT );

Int main ()
{
A A; // because the address of the member function must be included with the address of an object, we must create an object.
B;
P fun = & A: func1;

Cout <(A. * Fun) (4, 5) <Endl;
Cout <(B. * Fun) (4, 5) <Endl;

Fun = & A: func2;
Cout <(A. * Fun) () <Endl; // note that the virtual function is called here. really magic class member function pointers also support polymorphism.
Cout <(B. * Fun) (4, 5) <Endl;

// Fun = & B: func2; // This error is returned, because there is no implicit conversion from "pointer to class member function" of the derived class to "pointer to class member function" of the base class
Fun = (INT (A: *) (INT, INT) & B: func2; // forced conversion is required.
Cout <(A. * Fun) (4, 5) <Endl;
Cout <(B. * Fun) (4, 5) <Endl;
 
P0 fun0 = & B: func2;
Cout <(A. * Fun) (4, 5) <Endl;
Cout <(B. * Fun) (4, 5) <Endl;
 
Fun0 = & A: func2; // correct, because implicit conversion is performed here
Cout <(A. * Fun) (4, 5) <Endl;
Cout <(B. * Fun) (4, 5) <Endl;

// From the above, it is not difficult to find the relationship between the pointer base class and the derived class pointing to the class member function and the pointer base class pointing to the class object and the derived class,
// The layout of the base-class member functions is considered to be a subset of the layout of the member functions of the derived class.
Return 0;
}

Next is the usage of the class member function pointer of the template class.

Example:

# Include <tchar. h>
# Include <iostream>
# Include <stdio. h>
Using namespace STD;

Class
{
Public:
Int func (int x, int y)
{
Printf ("A: FUNC: x = % d, y = % d/N", x, y );
Return (x <Y? X: Y );
}
};
Class B
{
Public:
Int func (int x, int y)
{
Printf ("B: FUNC: x = % d, y = % d/N", x, y );
Return (x> Y? X: Y );
}
};

Template <class T>
Class C
{
Public:
T c;
Void print ()
{
INT (T: * p) (INT, INT) = & T: func;
(C. * p) (4, 5 );
}
};

Int main ()
{
C <A> Ca;
C <B> CB;

CA. Print ();
CB. Print ();
Return 0;
}
You can see clearly from above .. In fact, it is no different from a common template .. However, the parameter name is OK...

This article from http://blog.csdn.net/jinjinclouded/article/details/5189540

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.