C++, function pointer

0. Function pointers are among the most powerful tools in C.  It can be used to implement function callback in C. C++ takes a slightly different route for callbacks.

1. Function pointer

// (1)define a function

bool lengthCompare(const string&, const string&){

  return true;

}

// (2)define a function pointer and initialize it

bool (*pf) (const string&, const string&);

pf = lengthCompare;

// (3)call a function through a function pointer

cmpFcn pf = lengthCompare;  // initialize the pf

pf("hi","bye"); // implicitly dereferenced

(*pf)("hi","type"); // explicitly dereferenced

// (4)define a type named cmpFcn, this type is a pointer to function.

typedef bool (*cmpFcn) (const string&, const string&);

typedef int func(int*, int); // func is a function type, not a pointer to a function.

// (5)function pointer as a parameter

void useBigger(const string&, const string&, bool (*)(const string&, const string&));

void useBigger(const string&, const string&, bool (const string&, const string&));

// (6)function pointer as a return type

int (*ff(int))(int*, int); // is the same as following codes

typedef int (*pf)(int&, int);

pf ff(int); // ff return a pointer to function

// (7) example

int main(){

  cmpFcn pf1;
  cmpFcn pf2 = 0; 
  cmpFcn pf3 = lengthCompare;
  cmpFcn pf4 = &lengthCompare;

  pf = lengthCompare;
  cout << pf << endl;   // 1
  cout << pf1 << endl;  // 0
  cout << pf2 << endl;  // 0
  cout << pf3 << endl;  // 1
  cout << pf4 << endl;  // 1

  return 0;
}

2. There is no conversation between one pointer to function type and another.

A function pointer must point to the function whose type is exactly the same as this pointer points to.

( Most of the code are coming from C++ primer fourth edition  )

原文地址:https://www.cnblogs.com/sarah-zhang/p/12210931.html

时间: 2024-07-31 02:11:19

C++, function pointer的相关文章

类非静态成员的函数指针 的使用 Function pointer of a non-static member function of a class

you can get the pointer of the method, but it has to be called with an object typedef void (T::*MethodPtr) (); MethodPtr method = &T::MethodA; T *obj = new T(); obj->*method(); If you need to have non-object pointer and you want to use object then

tips~function pointer

An simple example: #include<stdio.h> int plus(int a,int b) { return a+b; } int main() { int (*func)(int,int); func=&plus; //point to the function ''plus(int,int)'' printf("the result is %d\n",(*func)(4,7)); return 0; } Another example:

function pointer x + y

#include <iostream> using namespace std; double add (double x, double y) { return x + y; } double calculate (double m, double n, double (*pf)(double, double)) { return (*pf)(m, n); } int main() { double sum = 0; double a, b; cin >> a >>

(C/C++) Callback Function

原文: http://www.codeguru.com/cpp/cpp/cpp_mfc/callbacks/article.php/c10557/Callback-Functions-Tutorial.htm Callback Functions Tutorial Introduction If you are reading this article, you probably wonder what callback functions are. This article explains

VC6.0 The value of ESP was not properly saved across a function call 错误解决方法

调用DLL函数,出现错误 Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling con

C++ virtual table pointer - vptr

To implement virtual functions, C++ uses a special form of late binding known as the virtual table. The virtual table is a lookup table of functions used to resolve function calls in a dynamic/late binding manner. The virtual table sometimes goes by

智能指针(smart pointer)(2):unique_ptr

Unique pointer: Manages the storage of a pointer, providing a limited garbage-collection facility, with little to no overhead over built-in pointers (depending on the deleter used). These objects have the ability of taking ownership of a pointer: onc

Callback Function

typedef void (*callbackFun)(int a, int b);struct exm { int type; callbackFun fun;}; A pointer is a special kind of variable that holds the address of another variable. The same concept applies to function pointers, except that instead of pointing to

c++ virturn function -- 虚函数

pure irtual function  -- 纯虚函数 先看例子 #include <iostream> using namespace std; class Polygon { protected: int width, height; public: void set_values (int a, int b) { width=a; height=b; } virtual int area() = 0 ;//{return 0;} // _vptr.Polygon show difre