//2020年05月23日 20时48分46秒
//professional c++ 4th edition p325
#include
#include
#include
#include <string_view>
#include
#include
using namespace std::rel_ops;
//虽然类中只重载了==和<两个关系操作符,但使用命名空间rel_ops后便可以得到6个。
class SpreadsheetCell{
public:
SpreadsheetCell() = default;
SpreadsheetCell(double initialValue);
explicit SpreadsheetCell(std::string_view initialValue);
void set(double inValue);
void set(std::string_view inString);
double getValue() const { mNumAccesses++; return mValue; }
std::string getString() const { mNumAccesses++; return doubleToString(mValue); }
static std::string doubleToString(double inValue);
static double stringToDouble(std::string_view inString);
SpreadsheetCell& operator+=(const SpreadsheetCell& rhs);
SpreadsheetCell& operator-=(const SpreadsheetCell& rhs);
SpreadsheetCell& operator*=(const SpreadsheetCell& rhs);
SpreadsheetCell& operator/=(const SpreadsheetCell& rhs);
//p325
SpreadsheetCell operator-() const;
SpreadsheetCell& operator++(); // prefix
SpreadsheetCell operator++(int); // postfix
SpreadsheetCell& operator--(); // prefix
SpreadsheetCell operator--(int); // postfix
private:
double mValue = 0;
mutable size_t mNumAccesses = 0;
};
//p327
std::ostream& operator<<(std::ostream& ostr, const SpreadsheetCell& cell);
std::istream& operator>>(std::istream& istr, SpreadsheetCell& cell);
-
SpreadsheetCell::SpreadsheetCell(double initialValue)
-
mValue(initialValue){
} SpreadsheetCell::SpreadsheetCell(std::string_view initialValue)
-
mValue(stringToDouble (initialValue)){
}
void SpreadsheetCell::set(double inValue)
{
mValue = inValue;
}
void SpreadsheetCell::set(std::string_view inString){
mValue = stringToDouble(inString);
}
std::string SpreadsheetCell::doubleToString(double inValue){
return std::__cxx11::to_string(inValue);
}
double SpreadsheetCell::stringToDouble(std::str