Function pointer Basics

Source: Internet
Author: User

# Include <iostream> <br/> using namespace STD; <br/> typedef void (* fcnptr) (Int &); // C ++ primer, this definition indicates that fcnptr is a pointer type Name Pointing to the function, this pointer is a pointer to a function with a return value of void and an int & parameter. <br/> // This method is useful for defining synonyms, because the function pointer type is often very lengthy <br/> // example function <br/> void print (Int & Data) <br/>{< br/> cout <data <Endl; <br/>}< br/> // example function <br/> void add_one (Int & Data) <br/>{< br/> data ++; <br/>}< br/> // generic container. To demonstrate how to encapsulate data and provide data utilization, <br/> temp Late <class T> <br/> class container <br/>{< br/> Public: <br/> container (); <br/> container (INT length, t arr []); <br/> void for_each (void (*) (T &); // provides an interface for Traversing data <br/> PRIVATE: <br/> int length; <br/> T * data; <br/> }; <br/> // define <br/> template <class T> <br/> container <t>: container () <br/>{< br/> length = 0; <br/> }; <br/> // define <br/> template <class T> <br/> container <t>: container (INT length, t arr []) <Br/>{< br/> This-> length = length; <br/> DATA = new T [This-> length]; <br/> for (INT I = 0; I <length; I ++) <br/>{< br/> data [I] = arr [I]; <br/>}< br/> //!!! Are there any comparisons? <Br/> // The parameter declared in the class is void (*) (T &) <br/> // void (* visit) (T &) is defined here &) <br/> // Lenovo ing... <br/> // previously, we can declare POP (INT) in this way, and then pop (INT t) during implementation) <br/> // Therefore, the parameter type is int, and the object name is T <br/> // so ..., likewise... <br/> // The above function pointer type is void (*) (T &), that is, return type + (*) + function parameter list <br/> // the pointer name is visit <br/> template <class T> <br/> void container <t> :: for_each (void (* visit) (T &) <br/>{< br/> for (INT I = 0; I <length; I ++) <br/>{< br/> visit (data [I]); <br/>}< br/> int main () <br/>{< br/> int arr [5] = {1, 2, 3, 4, 5 }; <br/> container <int> int_container (5, arr); <br/> int_container.for_each (print); // C ++ primer, when the function name is referenced but the function is not called, the function name is automatically interpreted as a pointer to the function <br/> fcnptr p = 0; // C ++ primer, function pointers can only be initialized or assigned values using the same type, function pointers, or 0-means expressions <br/> P = add_one; // Chen zhipeng, the same type indicates that the returned type is the same as the parameter list, so you can also p = print <br/> I Nt_container.for_each (p); // play, call add_one <br/> int_container.for_each (print); // play, call print to continue output <br/> return 0; <br/>}< br/> // thank you! <Br/>

all is over!

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.