use function pointer as parameter

本文通过一个简单的C++程序示例介绍了如何使用函数指针。示例中定义了两个函数fun1和fun2,并且创建了一个接受函数指针作为参数的函数fun。在主函数main中,分别将fun1和fun2传递给了fun函数进行调用。

//here is a simple demo:

#include <iostream>

using namespace std;

void fun(void (*p_fun)());//attention: here '(*p_fun)' ,the parentheres cannot be omitted

                                       //this means 'p_fun' is a function pointer
void fun1();
void fun2();
int main() {
    fun(fun1);
    fun(fun2);
    return 0;
}

void fun1(void) {
    cout<<"this is fun1!"<<endl;
}

void fun2(void) {
    cout<<"this is fun2!"<<endl;
}

void fun(void (*p_fun)()) {
    (*p_fun)();
}

The WriteFile function writes data to a file and is designed for both synchronous and asynchronous operation. The function starts writing data to the file at the position indicated by the file pointer. After the write operation has been completed, the file pointer is adjusted by the number of bytes actually written, except when the file is opened with FILE_FLAG_OVERLAPPED. If the file handle was created for overlapped input and output (I/O), the application must adjust the position of the file pointer after the write operation is finished.This function is designed for both synchronous and asynchronous operation. TheWriteFileEx function is designed solely for asynchronous operation. It lets an application perform other processing during a file write operation.BOOL WriteFile( HANDLE hFile, // handle to file LPCVOID lpBuffer, // data buffer DWORD nNumberOfBytesToWrite, // number of bytes to write LPDWORD lpNumberOfBytesWritten, // number of bytes written LPOVERLAPPED lpOverlapped // overlapped buffer);ParametershFile[in] Handle to the file to be written to. The file handle must have been created with GENERIC_WRITE access to the file.Windows NT/2000/XP: For asynchronous write operations, hFile can be any handle opened with the FILE_FLAG_OVERLAPPED flag by theCreateFile function, or a socket handle returned by thesocket or accept function.Windows 95/98/Me: For asynchronous write operations, hFile can be a communications resource opened with the FILE_FLAG_OVERLAPPED flag byCreateFile, or a socket handle returned by socket oraccept. You cannot perform asynchronous write operations on mailslots, named pipes, or disk files.lpBuffer[in] Pointer to the buffer containing the data to be written to the file.nNumberOfBytesToWrite[in] Specifies the number of bytes to write to the file.A value of zero specifies a null write operation. The behavior of a null write operation depends on the underlying file system. To truncate or extend a file, use theSetEndOfFile function.Named pipe write operations across a network are limited to 65,535 bytes.lpNumberOfBytesWritten[out] Pointer to the variable that receives the number of bytes written. WriteFile sets this value to zero before doing any work or error checking.Windows NT/2000/XP: If lpOverlapped is NULL, lpNumberOfBytesWritten cannot be NULL. IflpOverlapped is not NULL, lpNumberOfBytesWritten can be NULL. If this is an overlapped write operation, you can get the number of bytes written by callingGetOverlappedResult. If hFile is associated with an I/O completion port, you can get the number of bytes written by callingGetQueuedCompletionStatus.If I/O completion ports are used and you are using a callback routine to free the memory allocated to theOVERLAPPED structure pointed to by the lpOverlapped parameter, specify NULL as the value of this parameter to avoid a memory corruption problem during the deallocation. This memory corruption problem will cause an invalid number of bytes to be returned in this parameter.Windows 95/98/Me: This parameter cannot be NULL.lpOverlapped[in] Pointer to an OVERLAPPED structure. This structure is required ifhFile was opened with FILE_FLAG_OVERLAPPED.
11-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值