double转换为string 及代理构造函数

Posted on 2019-06-05 10:13  金色的省略号  阅读(164)  评论(0编辑  收藏  举报
 1 #include <iostream>
 2 /* #include <string>
 3 #include <sstream> */
 4 class C
 5 {
 6 int x = 0;  
 7 public:
 8     //1.
 9     C(int x):x{x}{
10         std::cout<<"C(" + std::to_string(x) + ")"<<"\n";
11     }
12 
13     //2.
14     C(double d):C(static_cast<int>(d)){    
15         std::string s = std::to_string(d);
16         std::cout<< "C(" + s.erase(s.find_last_not_of('0')+1) + ")" <<std::endl;
17         /* std::ostringstream oss; 
18         oss<<d; 
19         std::cout<< "C(" + oss.str() + ")" <<std::endl; */
20     }
21     
22     //3.
23     C():C(3.8){
24         std::cout<<"C()"<<std::endl;
25     }    
26 };
27 int main()
28 {
29     C c3;
30     C c2{2.2};
31     C c1{1};
32     return 0;
33 }