How to define a class member pointer (member variable or member function)

Source: Internet
Author: User

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.

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.