流对象不能直接做参数传递

ifstream、ofstream之类流对象不能直接做参数传递,像这样

void foo(ofstream os)
{
    // do something
}

int main()
{
    ofstream os("test.txt");
    foo(os);
    // ....
}

但只要将参数设置为引用参数就行了,像这样

void foo(ofstream &os)
{
    // do something
}

// .....


你可能感兴趣的:(流对象不能直接做参数传递)