且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用cin将输入作为函数参数传递

更新时间:2023-02-22 14:49:05

不是.现在,您可以将其放入一个函数中:

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));

但是,当然,这仍然会创建一个int(只是将其移至readInt函数).

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

实际上,您已经创建了一些对象/内存空间供int居住,因此您可以读取并传递它.您可以在此处进行更改.但是,只需回答您的问题即可:

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.