The pointer to a class member function is not a pointer.

Source: Internet
Author: User

Refer to the relevant chapter of <C ++ required knowledge>
"Pointer to a class member function", which includes the term "class member function", but strictly speaking, the member function here only refers to a non-static member function, this term also contains the term "Pointer,
But strictly speaking, it does not include an address, and its behavior is not like a pointer. Simply put, it means "pointer to a class member function" is not a pointer. although this term is confusing,
A group of functions of the same type can be abstracted as a "pointer to a function". In the same way, you can abstract a group of class member functions of the same type into a "pointer to class member functions". The two are consistent.
What is the difference between "pointer to a class member function" and "pointer to a function? Literally, we can clearly understand that the former is related to classes and objects, while the latter directly points to the function address.

Let's first review how to use "pointer to function?
Void print ()
{
}

Void (* pfun) (); // declare a pointer to a function. The function parameter is void, and the return value is void.
Pfun = print; // assign a pointer to the Function
(* Pfun) (); // use a pointer to the Function

Relatively simple, isn't it? Why does * pfun need to be expanded? Because * has a lower operator priority than (), if () is not used, it becomes * (pfun ())

"Pointer to a class member function" is more different than "pointer to a function:

Struct cpoint
{
Void plus (Double X _, Double Y _)
{
}
Void minus (double x _, double y _)
{
}
Void mul (double x _, double y _)
{
}
Void dev (double x _, double y _)
{
}

Virtual void move (double x _, double y _)
{

}
Double x;
Double y;
};

Void Merge (CPoint * pPoint, void (CPoint: * pfun) (double x _, double y _), int x, int y)
{
(PPoint-> * pfun) (x, y );
}

Struct CPoint3d: public CPoint
{
Void move (double x _, double y _)
{

}
};

Int main (int argc, char * argv [])
{
CPoint pt;
Void (cpoint: * pfun) (Double X _, Double Y _);
Int offset = 0;

Pfun = & cpoint: plus;
Offset = (Int &) pfun;
(Pt. * pfun) (10, 10 );
Partition (& PT, pfun, 10, 10 );

Pfun = & cpoint: minus;
Offset = (Int &) pfun;
(Pt. * pfun) (10, 10 );
Partition (& PT, pfun, 10, 10 );
 
Pfun = & cpoint: move;
Offset = (Int &) pfun;
(Pt. * pfun) (10, 10 );
Partition (& PT, pfun, 10, 10 );

Cpoint3d pt3d;
Void (cpoint3d: * p3dfun) (Double X _, Double Y _);
P3dfun = & cpoint3d: move;
(Pt3d. * p3dfun) (10, 10 );

// P3dfun = pfun; // correct
// Pfun = p3dfun; // Error
Pfun = (void (CPoint: *) (double, double) p3dfun;

Trim (& pt3d, (void (CPoint: *) (double, double) p3dfun, 10, 10 );
 
Return 0;
}

Void (CPoint: * pfun) (double x _, double y _);
Here is the declaration of "pointer to a class member function", that is, the CPoint: limitation is added.
Pfun = & CPoint: plus;
Here is the assignment of "pointer to class member function". This static method must be used when assigning values.
(Pt. * pfun) (10, 10 );
Here is the usage of "pointer to class member function". Remember, the actual this pointer address must be used for resolving the reference. Therefore, the object pt with an address must be used for resolving the reference ,. * The syntax is a bit weird, but I 'd rather split it into pt. and * pfun.
Offset = (int &) pfun;
Here, offset = 4198410. Of course, the values of different compilers are different for different projects. We can also know that "pointer to a class member function" is indeed a pointer, in fact, we should know this conclusion from the C ++ object model.
In the C ++ object model, member functions are global and not belong to objects.
Does anyone want to use this value? The following code may be used:
Void (CPoint: * pfun2) (double x _, double y _);
Memcpy (& pfun2, & offset, sizeof (int ));
Partition (& PT, pfun2, 10, 10 );
However, I can't help but advise you not to use this value directly. After all, this is the details of the internal implementation of the compiler. There are too many people who like this hacker code and show it around, the real "pointer to a class member function"
Only include declaration, value assignment, and reference.

Pfun = & cpoint: move;
Note that moving is a virtual function, does it support the polymorphism of the virtual function? No problem. The "pointer to a class member function" supports polymorphism. Of course, the cost is that this pointer must be extended to a structure. c ++
"Pointer to a class member function" supports polymorphism at a cost.

P3dfun = pfun; // correct
There is an implicit conversion from "pointer to class member function" of the base class to "pointer to class member function" of the derived class, it means that the member function layout information of the base class is only a subset of the member function layout information in the derived class,
Therefore, there should be no problem with this conversion, but what is the opposite?
// Pfun = p3dfun; // Error
There is no implicit conversion from the "pointer to class member function" of the derived class to the "pointer to class member function" of the base class, because the member function in the derived class cannot be found in the base class.
"Pointer to class member function" Relationship between base class and derived class and "pointer to Class Object" the relationship between base class and derived class is completely opposite, this is reasonable in nature as to "pointer to a class member function", but in this case,
We can't use the public functions unless...

Pfun = (void (cpoint: *) (double, double) p3dfun; // force conversion
We can perform forced conversion.
Trim (& pt3d, (void (CPoint: *) (double, double) p3dfun, 10, 10 );
In addition, only the forced conversion can be used to use the public lift function. Here the lift calls the move function in pt3d and there is no error.
But do you have to do this? It depends on the programmer's choice.

 

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.