dir.by  
  Search  
Programming, development, testing
C++
Pass a function as a parameter to a function (callback) | C++
  Looked at 981 times    
 Pass a function as a parameter to a function (callback) | C++ 
last updated: 5 December 2024
There is such a type of data as a function:
std::function< Return Type (Type1, Type2...) >


To use it, you need to add a library:
#include <functional>
 
Explanation
In C++, we can make a variable with a simple type, for example:
int is a number
int myPrice;    // made the variable myPrice
myPrice = 10;    // variable set to 10
bool is yes or no
bool hasBook;    // made the variable hasBook
hasBook = true;    // variable is set to true
In C++, we can make a variable with a complex type, for example:
std::string is the text

Note! To use, you need to add:
#include <string>
#include <string>

std::string myName;    // made the variable myName
myName = "Evgen";    // variable set to "Evgen"
In C++, we can make a variable with a very complex type, for example:
std::function<return type(type1, type2, ...)> is a function
#include <functional>

std::function<void(int)> func1;    // made the variable func1

func1 = [](int a) { // variable was set to the following function
     std::cout << a; // Show the value of the variable a
};
Example 1
  C++  
#include <iostream>
#include <string>
#include <functional>

int main()
{
     // declare the variable func1
     std::function<void(std::string)> func1;

     // set the value (function body) to the variable func1
     func1 = [](std::string text) {
          std::cout << text;
     };

     // func1 This variable indicates a function
     func1("Hello!");
     func1("Thank you");
}
Example 2 Pass a function as a parameter to a function (callback)
  C++  
#include <iostream>

#include <string>
#include <functional>
#include <fstream>

// The ReadTextFile function contains 2 parameters:
// 1st peremeter is filename (text)
// 2nd peremeter is callback (function)
void ReadTextFile(const char* filename, std::function<void(std::string)> callback)
{
     std::ifstream file;
     file.open(filename);
     if (!file)
          return;

     std::string line;
     while (file >> line) {
          callback(line);
     }

     file.close();
}

int main()
{
     // The second parameter is to pass the unnamed function
     ReadTextFile("d://1.txt", [](std::string line) {
          std::cout << line;
     }
);
}
 
← Previous topic
std::map<Key, Value> is a set of keys and values in C++. An important feature of std::map is to quickly find a value by key
 
Next topic →
Create a new OpenGL application on C++ | Library GLUT, Visual Studio
 
Your feedback ... Comments ...
   
Your Name
Your comment (www links can only be added by a logged-in user)

  Объявления  
  Объявления  
 
dynamic_cast in C++ (converting a pointer to a different type and checking validity in runtime)
std::map<Key, Value> is a set of keys and values in C++. An important feature of std::map is to quickly find a value by key
Pass a function as a parameter to a function (callback) | C++
OpenGL
Create a new OpenGL application on C++ | Library GLUT, Visual Studio
Creating a OpenGL app with 3D pyramid drawing and a motion camera on C++ | Visual Studio | Library glut for using OpenGL
Create a new OpenGL ES2 app on C++ in Windows | Visual Studio, Desktop application

  Ваши вопросы присылайте по почте: info@dir.by  
Яндекс.Метрика