板凳——————————————————c++(33)

//professional c++ 4th edition p 331
#include

class FunctionObject{
public:
int operator() (int param);
int doSquare (int param);
};

int FunctionObject::operator() (int param){
return doSquare(param);
}

int FunctionObject::doSquare(int param){
return param * param;
}

int main(){
int x = 3, xSquared, xSquaredAgain;
FunctionObject square;
xSquared = square(x);
xSquaredAgain = square.doSquare(x);
}

//professional c++ 4th edition p332
#include
#include
#include
#include
#include
#include <string_view>
#include
#include
using namespace std::rel_ops;

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
     //p335
     //operator double() const;              // 问题出在这!
     operator std::string() const;

/*p497
The usual pre-C++11 solution to this conundrum is to make the constructor in question explicit, so that
the automatic conversion using that constructor is prevented(see Chapter 9). However, you don’t want
that constructor to be explicit because you generally like the automatic conversion of doubles to
SpreadsheetCells. Since C++11, you can solve this problem by making the double conversion operator explicit
instead of the constructor: //

     explicit operator double() const;       // 此处与上行 矛盾
     

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);
//p336
bool operator==(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs);
bool operator<(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs);
bool operator>(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs);
bool operator!=(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs);
bool operator<=(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs);
bool operator>=(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs);

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::string_view inString){
return strtod(inString.data(), nullptr);
}

SpreadsheetCell operator+(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs){
return SpreadsheetCell(lhs.getValue() + rhs.getValue());
}

SpreadsheetCell operator-(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs){
return SpreadsheetCell(lhs.getValue() - rhs.getValue());
}

SpreadsheetCell operator*(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs){
return SpreadsheetCell(lhs.getValue() * rhs.getValue());
}

SpreadsheetCell operator/(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs){
if(rhs.getValue() == 0){
throw std::invalid_argument(“Divide by zero.”);
}
return SpreadsheetCell(lhs.getValue() / rhs.getValue());
}

SpreadsheetCell& SpreadsheetCell::operator+=(const SpreadsheetCell& rhs){
set(getValue() + rhs.getValue());
return *this;
}

SpreadsheetCell& SpreadsheetCell::operator-=(const SpreadsheetCell& rhs){
set(getValue() - rhs.getValue());
return *this;
}

SpreadsheetCell& SpreadsheetCell::operator*=(const SpreadsheetCell& rhs){
set(getValue() * rhs.getValue());
return *this;
}

SpreadsheetCell& SpreadsheetCell::operator/=(const SpreadsheetCell& rhs){
if (rhs.getValue() == 0) {
throw std::invalid_argument(“Divide by zero.”);
}
set(getValue() / rhs.getValue());
return *this;
}

bool operator==(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs){
return (lhs.getValue() == rhs.getValue());
}

bool operator<(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs){
return (lhs.getValue() < rhs.getValue());
}
//p325
SpreadsheetCell SpreadsheetCell::operator-() const
{
return SpreadsheetCell(-getValue());
}

SpreadsheetCell& SpreadsheetCell::operator++()
{
set(getValue() + 1);
return *this;
}

SpreadsheetCell SpreadsheetCell::operator++(int)
{
auto oldCell(*this); // Save current value
++(*this); // Increment using prefix ++
return oldCell; // Return the old value
}

SpreadsheetCell& SpreadsheetCell::operator–()
{
set(getValue() - 1);
return *this;
}

SpreadsheetCell SpreadsheetCell::operator–(int)
{
auto oldCell(*this); // Save current value
–(*this); // Decrement using prefix –
return oldCell; // Return the old value
}
//p328
std::ostream& operator<<(std::ostream& ostr, const SpreadsheetCell& cell)
{
ostr << cell.getValue();
return ostr;
}

std::istream& operator>>(std::istream& istr, SpreadsheetCell& cell)
{
double value;
istr >> value;
cell.set(value);
return istr;
}
//p334
SpreadsheetCell::operator double() const{ //(1)
return getValue();
}

SpreadsheetCell::operator std::string() const{ //(1)
return doubleToString(getValue());
}
//p336
bool operator>(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs)
{
return (lhs.getValue() > rhs.getValue());
}

bool operator!=(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs)
{
return (lhs.getValue() != rhs.getValue());
}

bool operator<=(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs)
{
return (lhs.getValue() <= rhs.getValue());
}

bool operator>=(const SpreadsheetCell& lhs, const SpreadsheetCell& rhs)
{
return (lhs.getValue() >= rhs.getValue());
}

template class Pointer{
public:
Pointer(T* ptr);
virtual ~Pointer();
Pointer(const Pointer& src) = delete;
Pointer& operator = (const Pointer& rhs) = delete;
//p333
T& operator*();
const T& operator*() const;
T* operator->();
const T* operator->() const;
//p336
operator void* () const;
//operator bool () const;
private:
T* mPtr = nullptr; // mPtr 打错了 mptr
};

template Pointer::Pointer(T* ptr) : mPtr(ptr){

}

template Pointer::~Pointer(){
delete mPtr;
mPtr = nullptr;
}

template T& Pointer::operator*()
{
return *mPtr;
}

template const T& Pointer::operator*() const
{
return *mPtr;
}

template T* Pointer::operator->()
{
return mPtr;
}

template const T* Pointer::operator->() const
{
return mPtr;
}
void testConst(const Pointer& p)
{
std::cout << *p;
// *p = 7;
}

void TestConstTwo(const Pointer& p)
{
// p->set(5);
}
//p336
template Pointer::operator void* () const
{
return mPtr;
}

//template Pointer::operator bool () const
//{
// return mPtr != nullptr;
//}
void process(Pointer& p){
if(p != nullptr) {std::cout << “not nullptr” << std::endl;}
if(p != NULL) {std::cout << “not NULL” << std::endl;}
if§ {std::cout << “not NULL” << std::endl;}
if(!p) {std::cout << “nullptr” << std::endl;}
}
int main(){//p333
Pointer smartInt(new int);
*smartInt = 5;
std::cout << *smartInt << std::endl;

Pointer<SpreadsheetCell> smartCell1 (new SpreadsheetCell);
smartCell1->set(5);
std::cout << smartCell1->getValue() << std::endl;
//p334
SpreadsheetCell myCell1;
double (SpreadsheetCell::*methodPtr) () const = &SpreadsheetCell::getValue;
std::cout << (myCell1.*methodPtr)() << std::endl;

SpreadsheetCell* myCell2 = new SpreadsheetCell();
std::cout << (myCell2->*methodPtr)() << std::endl;

SpreadsheetCell cell1(1.23);                       //(1)
//double d1 = cell1;          // operator double() const;  Works as expected
//std::string str1 = cell1; 

//double d2 = cell1 + 3.3;   //Do not complie if you define operator double()
//p335        
SpreadsheetCell cell3 (6.6);                       //[1]
std::string str2 = cell3;                          //[2]
double d3 = static_cast<double>(cell3);           //[3]
double d4 = static_cast<double>(cell3 + 3.3);     //[4]
std::cout << str2 << std::endl;
std::cout << d3 << std::endl;
std::cout << d4 << std::endl;

/*p497
//[1]Uses the implicit conversion from a double to a SpreadsheetCell. Because this is in the declaration,
this is done by calling the constructor that accepts a double.
//[2]Uses the operator string() conversion operator.
//[3]Uses the operator double() conversion operator. Note that because this conversion operator is now
declared explicit, the cast is required.
//[4]Uses the implicit conversion of 3.3 to a SpreadsheetCell, followed by operator+ on two SpreadsheetCells
, followed by a required explicit cast to invoke operator double().

Pointer<SpreadsheetCell> smartCell2(nullptr);
process(smartCell2);
std::cout << std::endl;

Pointer<SpreadsheetCell> anotherSmartCell(new SpreadsheetCell(5.0));
process(anotherSmartCell);
return 0;

}
/*
wannian07@wannian07-PC:~$ g++ -std=c++17 -o c17 c17.cpp
wannian07@wannian07-PC:~$ ./c17
5
5
0
0
6.600000
6.6
9.9
nullptr

not nullptr
not NULL
not NULL

*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值