Effective C++ 中好像有错误

sunrainchy 2014-07-25 05:41:40
在《Effective C++》 中文版 第三版 item35, 175页 第三句代码
EyeCandyCharacter ecc1(HealthCalculator());这句好像是错的,作者目的是定义一个类对象 ecc1
但是这里编译器会认为这一句代码是函数声明。

不知道看过 Effective C++的同学是否认同
...全文
385 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunrainchy 2014-07-27
  • 打赏
  • 举报
回复
引用 4 楼 dataxdata 的回复:
报错是因为cal类只定义了cal(myfunc)构造函数,并没有定义cal(MyClass)构造函数
好像不是这样的,如果把注释代码下一行注释掉那么就不报错,显然是把注释部分当声明了
dataxdata 2014-07-25
  • 打赏
  • 举报
回复
报错是因为cal类只定义了cal(myfunc)构造函数,并没有定义cal(MyClass)构造函数
sunrainchy 2014-07-25
  • 打赏
  • 举报
回复
引用 2 楼 dataxdata 的回复:
没问题的,函数声明必须有参数的类型,比如: MyClass test1(int); // test1是一个函数,参数是int,返回值是MyClass int GetIntValue() { return 1; } MyClass test2(GetIntValue()); // test2是类MyClass的一个实例,用函数GetIntValue的返回值进行初始化
那你帮我看下这段代码,注释部分会报错 #include <iostream> #include <algorithm> #include <iomanip> #include <string.h> #include <stdio.h> #include <tr1/memory> #include <tr1/functional> using namespace std; class cal; typedef std::tr1::function<int (cal&) > myfun; class myClass{ public: int operator()(cal& p){ printf("nihao\n"); return 0; } }; class cal{ public: cal(myfun f):fun(f){ } int print(){ cout<<fun(*this)<<endl; return 0; } private: myfun fun; }; int first(cal&p){ printf("nidaye\n"); return 0; } int main(){ cal a(first); a.print(); myClass ha; cal b(ha); //cal b(myClass()); b.print(); return 0; }
dataxdata 2014-07-25
  • 打赏
  • 举报
回复
没问题的,函数声明必须有参数的类型,比如: MyClass test1(int); // test1是一个函数,参数是int,返回值是MyClass int GetIntValue() { return 1; } MyClass test2(GetIntValue()); // test2是类MyClass的一个实例,用函数GetIntValue的返回值进行初始化
sunrainchy 2014-07-25
  • 打赏
  • 举报
回复
知道的同学给回一个嘛,一起学习,分什么的都是小事

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧