c/c++:c++ function pointer

Source: Internet
Author: User


C + + function pointers:

Let me first write a class object that specifies that the function pointer must be a member of the object, or a member of its subclass.


Class object{public    :        void (object::* p) ();};


OK, then we'll write a class Hello inherits from Class object. Write a constructor that points to the output object type using a function pointer.

#include <iostream>class object{public:    void (object::* p) ();}; Class Hello:public Object{public:    hello () {        p = (void (object::*) ())) (&hello::say_hello);        The type that is cast to object. (        this->*p) ();//points to the position of P, and cuts calls to it.    }    void Say_hello () {        std::cout << "Hello world!" << Std::endl;    }; int main () {    Hello *p = new Hello ();    Delete p;    return 0;}









c/c++:c++ 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.