Passing input as a function argument using cin

帅比萌擦擦* 提交于 2019-11-30 21:42:17

Nope. Now, you could put this into a function:

int readInt(std::istream& stream)
{
    int i;
    stream >> i; // Cross your fingers this doesn't fail
    return i;
}

// Then in your code:
obj.changeval(readInt(std::cin));

But of course, this still creates an int (it just moves it to the readInt function).

In reality, you have to create some object/memory space for the int to live in, so you can read it and pass it. Where you do this can be changed. But to simply answer your question: no.

You may do it like this:

void changeval(istream& in) { in >> k; }
...
changeval(cin);

Is that what you need?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!