小编Ric*_*rio的帖子

c ++将函数作为参数传递给另一个带有void指针的函数

我正在尝试将函数作为参数传递给另一个带有void指针的函数,但它不起作用

#include <iostream>
using namespace std;

void print()
{
    cout << "hello!" << endl;
}

void execute(void* f()) //receives the address of print
{
    void (*john)(); // declares pointer to function
    john = (void*) f; // assigns address of print to pointer, specifying print returns nothing
    john(); // execute pointer
}

int main()
{
    execute(&print); // function that sends the address of print
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

事情是void函数指针,我可以做一个更简单的代码

#include <iostream>
using namespace std;

void print();
void execute(void());

int main()
{
    execute(print); …
Run Code Online (Sandbox Code Playgroud)

c++ pointers arguments function void

4
推荐指数
1
解决办法
7963
查看次数

如何将通用文件流作为参数传递?

我有这个功能:

void check_open (ifstream& file)
{
    if (not file.is_open())
    {
        cout << "Error." << endl;
        exit(1);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是我只能传递ifstream参数,我怎么能让它接受ofstream参数呢?

c++ fstream stream

1
推荐指数
1
解决办法
1931
查看次数

标签 统计

c++ ×2

arguments ×1

fstream ×1

function ×1

pointers ×1

stream ×1

void ×1