(1)用stringstream的时候, 如果你想重复用相同的变量, 交替的使用<<和>>, 那么要记住
stringstream ss;
....
ss.clear() //to clear flags if reusing
(2)
{
public:
int getInch();
bool equals(const Length& len);
}
bool Length::equals(const Length& len)
{
int inch= len.getInch(); //what is wrong?
....
....
}
上面的程序会出现一个编译错误,为什么?
error: passing `const Length' as `this' argument of `int Length::getInch()' discards qualifiers ???
因为你在使用参数 const Length &len的时候, 你发誓你不会改变len, 但是编译器并不信任你, 因为
你没有在 equals那个函数那里做同样的发誓。
int getInch() const
to bool Length::equals(Length len)
{
int inch= len.inch; //reference member directly
....
....
}
stringstream ss;
....
ss.clear() //to clear flags if reusing
(2)
{
public:
int getInch();
bool equals(const Length& len);
}
bool Length::equals(const Length& len)
{
int inch= len.getInch(); //what is wrong?
....
....
}
上面的程序会出现一个编译错误,为什么?
error: passing `const Length' as `this' argument of `int Length::getInch()' discards qualifiers ???
因为你在使用参数 const Length &len的时候, 你发誓你不会改变len, 但是编译器并不信任你, 因为
你没有在 equals那个函数那里做同样的发誓。
- Solution1:
int getInch() const
- Solution2: (not recommended)
to bool Length::equals(Length len)
- Solution3:(not recommended)
{
int inch= len.inch; //reference member directly
....
....
}