鱼C论坛

 找回密码
 立即注册
查看: 1432|回复: 4

[已解决]c++ 转换运算符问题

[复制链接]
发表于 2023-1-22 21:59:22 | 显示全部楼层 |阅读模式
10鱼币
我想能直接输出类,但是,如果返回const char* 可以运行,但是返回string就报错
所以cout 输出字符串的时候是把字符串看做char*插入到流中的吗,string不行吗

  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>

  4. using namespace std;

  5. class Date
  6. {
  7.     int day,month,year;
  8.     string s;
  9. public:
  10.     Date()
  11.     {

  12.     }
  13.     Date(int pday,int pmonth,int pyear)
  14.     {
  15.         day = pday;
  16.         month = pmonth;
  17.         year = pyear;
  18.     }
  19.     Date(Date& d)
  20.     {
  21.         day = d.day;
  22.         month = d.month;
  23.         year = d.year;
  24.     }
  25.     ~Date()
  26.     {

  27.     }
  28.     /*operator string()
  29.     {
  30.         ostringstream p;
  31.         p << year << "," << month << "," << day;
  32.         s = p.str();
  33.         return s;
  34.     }*/
  35.     operator const char*()
  36.     {
  37.         ostringstream p;
  38.         p << year << "," << month << "," << day;
  39.         return p.str().c_str();
  40.     }
  41. };

  42. int main()
  43. {
  44.     Date d(29,2,2004);

  45.     cout << d << endl;

  46.     return 0;
  47. }
复制代码
最佳答案
2023-1-22 21:59:23
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>

  4. using namespace std;

  5. class A {};

  6. ostream &operator<<(ostream &os, const A &a) {
  7.     return os;
  8. }

  9. class Date {
  10.     int day, month, year;
  11.     string s;
  12. public:
  13.     Date(int pday, int pmonth, int pyear) {
  14.         day = pday;
  15.         month = pmonth;
  16.         year = pyear;
  17.     }
  18.     Date(Date &d) {
  19.         day = d.day;
  20.         month = d.month;
  21.         year = d.year;
  22.     }
  23.     /*
  24.     operator const string() {
  25.         ostringstream p;
  26.         p << year << "," << month << "," << day;
  27.         s = p.str();
  28.         return s;
  29.     }
  30.     */
  31.     /*
  32.     operator const char* ()
  33.     {
  34.         ostringstream p;
  35.         p << year << "," << month << "," << day;
  36.         return p.str().c_str();
  37.     }
  38.     */
  39.     operator const A() {
  40.         return A();
  41.     }
  42. };

  43. int main() {
  44.     Date d(29, 2, 2004);
  45.     cout << d << endl;
  46.     return 0;
  47. }
复制代码


上面这个代码可以通过编译,因为有这两个
operator const A();
ostream &operator<<(ostream &os, const A &a);

  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>

  4. using namespace std;

  5. class A {};

  6. ostream &operator<<(ostream &os, const A &a) {
  7.     return os;
  8. }

  9. class Date {
  10.     int day, month, year;
  11.     string s;
  12. public:
  13.     Date(int pday, int pmonth, int pyear) {
  14.         day = pday;
  15.         month = pmonth;
  16.         year = pyear;
  17.     }
  18.     Date(Date &d) {
  19.         day = d.day;
  20.         month = d.month;
  21.         year = d.year;
  22.     }
  23.     operator const string() {
  24.         ostringstream p;
  25.         p << year << "," << month << "," << day;
  26.         s = p.str();
  27.         return s;
  28.     }
  29.     /*
  30.     operator const char* ()
  31.     {
  32.         ostringstream p;
  33.         p << year << "," << month << "," << day;
  34.         return p.str().c_str();
  35.     }
  36.     */
  37.     /*
  38.     operator const A() {
  39.         return A();
  40.     }
  41.     */
  42. };

  43. ostream &operator<<(ostream &os, const string &s) {
  44.     return os;
  45. }

  46. int main() {
  47.     Date d(29, 2, 2004);
  48.     cout << d << endl;
  49.     return 0;
  50. }
复制代码


上面这个代码也可以通过编译
因为这两个
ostream &operator<<(ostream &os, const string &s);
operator const string();

  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>

  4. using namespace std;

  5. class A {};

  6. ostream &operator<<(ostream &os, const A &a) {
  7.     return os;
  8. }

  9. class Date {
  10.     int day, month, year;
  11.     string s;
  12. public:
  13.     Date(int pday, int pmonth, int pyear) {
  14.         day = pday;
  15.         month = pmonth;
  16.         year = pyear;
  17.     }
  18.     Date(Date &d) {
  19.         day = d.day;
  20.         month = d.month;
  21.         year = d.year;
  22.     }
  23.     operator const string() {
  24.         ostringstream p;
  25.         p << year << "," << month << "," << day;
  26.         s = p.str();
  27.         return s;
  28.     }
  29.     /*
  30.     operator const char* ()
  31.     {
  32.         ostringstream p;
  33.         p << year << "," << month << "," << day;
  34.         return p.str().c_str();
  35.     }
  36.     */
  37.     /*
  38.     operator const A() {
  39.         return A();
  40.     }
  41.     */
  42. };

  43. int main() {
  44.     Date d(29, 2, 2004);
  45.     cout << d << endl;
  46.     return 0;
  47. }
复制代码

上面这段代码就无法通过编译了,因为没有这个
ostream &operator<<(ostream &os, const string &s);

最佳答案

查看完整内容

上面这个代码可以通过编译,因为有这两个 operator const A(); ostream &operator
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-1-22 21:59:23 | 显示全部楼层    本楼为最佳答案   
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>

  4. using namespace std;

  5. class A {};

  6. ostream &operator<<(ostream &os, const A &a) {
  7.     return os;
  8. }

  9. class Date {
  10.     int day, month, year;
  11.     string s;
  12. public:
  13.     Date(int pday, int pmonth, int pyear) {
  14.         day = pday;
  15.         month = pmonth;
  16.         year = pyear;
  17.     }
  18.     Date(Date &d) {
  19.         day = d.day;
  20.         month = d.month;
  21.         year = d.year;
  22.     }
  23.     /*
  24.     operator const string() {
  25.         ostringstream p;
  26.         p << year << "," << month << "," << day;
  27.         s = p.str();
  28.         return s;
  29.     }
  30.     */
  31.     /*
  32.     operator const char* ()
  33.     {
  34.         ostringstream p;
  35.         p << year << "," << month << "," << day;
  36.         return p.str().c_str();
  37.     }
  38.     */
  39.     operator const A() {
  40.         return A();
  41.     }
  42. };

  43. int main() {
  44.     Date d(29, 2, 2004);
  45.     cout << d << endl;
  46.     return 0;
  47. }
复制代码


上面这个代码可以通过编译,因为有这两个
operator const A();
ostream &operator<<(ostream &os, const A &a);

  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>

  4. using namespace std;

  5. class A {};

  6. ostream &operator<<(ostream &os, const A &a) {
  7.     return os;
  8. }

  9. class Date {
  10.     int day, month, year;
  11.     string s;
  12. public:
  13.     Date(int pday, int pmonth, int pyear) {
  14.         day = pday;
  15.         month = pmonth;
  16.         year = pyear;
  17.     }
  18.     Date(Date &d) {
  19.         day = d.day;
  20.         month = d.month;
  21.         year = d.year;
  22.     }
  23.     operator const string() {
  24.         ostringstream p;
  25.         p << year << "," << month << "," << day;
  26.         s = p.str();
  27.         return s;
  28.     }
  29.     /*
  30.     operator const char* ()
  31.     {
  32.         ostringstream p;
  33.         p << year << "," << month << "," << day;
  34.         return p.str().c_str();
  35.     }
  36.     */
  37.     /*
  38.     operator const A() {
  39.         return A();
  40.     }
  41.     */
  42. };

  43. ostream &operator<<(ostream &os, const string &s) {
  44.     return os;
  45. }

  46. int main() {
  47.     Date d(29, 2, 2004);
  48.     cout << d << endl;
  49.     return 0;
  50. }
复制代码


上面这个代码也可以通过编译
因为这两个
ostream &operator<<(ostream &os, const string &s);
operator const string();

  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>

  4. using namespace std;

  5. class A {};

  6. ostream &operator<<(ostream &os, const A &a) {
  7.     return os;
  8. }

  9. class Date {
  10.     int day, month, year;
  11.     string s;
  12. public:
  13.     Date(int pday, int pmonth, int pyear) {
  14.         day = pday;
  15.         month = pmonth;
  16.         year = pyear;
  17.     }
  18.     Date(Date &d) {
  19.         day = d.day;
  20.         month = d.month;
  21.         year = d.year;
  22.     }
  23.     operator const string() {
  24.         ostringstream p;
  25.         p << year << "," << month << "," << day;
  26.         s = p.str();
  27.         return s;
  28.     }
  29.     /*
  30.     operator const char* ()
  31.     {
  32.         ostringstream p;
  33.         p << year << "," << month << "," << day;
  34.         return p.str().c_str();
  35.     }
  36.     */
  37.     /*
  38.     operator const A() {
  39.         return A();
  40.     }
  41.     */
  42. };

  43. int main() {
  44.     Date d(29, 2, 2004);
  45.     cout << d << endl;
  46.     return 0;
  47. }
复制代码

上面这段代码就无法通过编译了,因为没有这个
ostream &operator<<(ostream &os, const string &s);
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-1-22 22:35:57 | 显示全部楼层
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>

  4. using namespace std;

  5. class Date
  6. {
  7.     int day,month,year;
  8.     string s;
  9. public:
  10.     Date()
  11.     {

  12.     }
  13.     Date(int pday,int pmonth,int pyear)
  14.     {
  15.         day = pday;
  16.         month = pmonth;
  17.         year = pyear;
  18.     }
  19.     Date(Date& d)
  20.     {
  21.         day = d.day;
  22.         month = d.month;
  23.         year = d.year;
  24.     }
  25.     ~Date()
  26.     {

  27.     }
  28.     operator string()
  29.     {
  30.         ostringstream p;
  31.         p << year << "," << month << "," << day;
  32.         s = p.str();
  33.         return s;
  34.     }
  35.     /*
  36.     operator const char*()
  37.     {
  38.         ostringstream p;
  39.         p << year << "," << month << "," << day;
  40.         return p.str().c_str();
  41.     }
  42.     */
  43. };

  44. int main()
  45. {
  46.     Date d(29,2,2004);

  47.     cout << (string)d << endl;

  48.     return 0;
  49. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-1-22 23:01:27 | 显示全部楼层

我想问一下为啥char*不用这样写,string要这样写
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-1-23 10:03:28 From FishC Mobile | 显示全部楼层
不是回答问题,只是好奇为什么不重载运算符(<<)而是自定义类型转换来实现这个效果?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-3 06:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表