First, let's take a look at what I have referenced online:
"The pointer format to the data member is as follows:
<Type description> <class name >:* <pointer Name>
The pointer format to a member function is as follows:
<Type description> (<class name >:* <pointer Name>) (<parameter table>)
For example, there is a Class:
Class
{
Public:
Int fun (int B) {return a * C + B ;}
A (int I) {A = I ;}
Int C;
PRIVATE:
Int;
};
Define a pointer PC pointing to Class A's data member C. Its format is as follows:
Int A: * Pc = & A: C;
Define a pointer to the member function fun of Class A in the following format:
INT (A: * pfun) (INT) = A: fun;
"
//////////////////////////////////////// /////////////////////////////////////////
(I was puzzled to see these similar things in the book, so I found the things online ~ The following is my own program. It has been compiled under vc6, but it cannot be compiled under Dev-CPP ~~ Very depressing ~)
# Include <iostream>
Using namespace STD;
Class
{
Public:
Virtual void F ()
{
Cout <"A: F was called" <Endl;
}
};
Class B: public
{
Public:
Virtual void F ()
{
Cout <"B: F was called" <Endl;
}
};
Int main ()
{
B;
Void (A: * PF) () = A: F;
(B. * PF )();
Cin. Get ();
Return 0;
}
With pointers, you must reference them through a specific class! Because the referenced object is not a specific object. therefore, PF does not have an exact address. it is an address relative to an object. if Staic is added before a member function, it can be processed as a normal function pointer.