C++ 这个指针对stringstream有什么作用?

C++ 这个指针对stringstream有什么作用?,c++,C++,这有什么用?他为什么用“>>*这个” 术语std::stringstream(计算)从calculation构造一个std::stringstream对象。大概,calculation是一个std::string对象 *此计算结果为对当前对象的引用 声明 std::stringstream(calculation) >> *this; 从计算中提取数据并填充当前对象。为此,必须重载对象类型的操作符>>函数 如果要处理的对象类型是Foo,请查找具有以下界面的函数: std::istr

这有什么用?他为什么用“>>*这个”


术语
std::stringstream(计算)
calculation
构造一个
std::stringstream
对象。大概,
calculation
是一个
std::string
对象

*此
计算结果为对当前对象的引用

声明

std::stringstream(calculation) >> *this;
计算中提取数据
并填充当前对象。为此,必须重载对象类型的
操作符>>
函数

如果要处理的对象类型是
Foo
,请查找具有以下界面的函数:

std::istream& operator>>(std::istream& in, Foo& foo) { ... }

我会使用
std::istringstream
和两行程序。我认为它使意图更清晰,代码更容易理解

std::istringstream str{calculation};
str >> *this;

这将创建一个用
calculation
初始化的
std::stringstream
对象,然后在当前类的
>
操作符上运行它。查找类似于
operator>>(std::stringstream&s,T&obj)
的内容,以了解它的作用。至于具体内容,我们无法确切说明,因为这取决于此
所指向的内容。请创建一个链接以显示给我们。另外,请阅读,以及。你可以看看它。它取决于您的
这个
指针,这个重载操作符的哪个版本将被使用called@SpentDeath可能更像
操作符>>(std::istream&,…)
。很少使用特定的流进行重载。对不起,我更新了函数。请看一下当前的对象是什么?计算?istream操作符对此函数做了什么?@Coder32,
是指向当前对象的指针,即调用成员函数的对象。看见
std::istringstream str{calculation};
str >> *this;