一减一 发表于 2023-1-22 21:59:22

c++ 转换运算符问题

我想能直接输出类,但是,如果返回const char* 可以运行,但是返回string就报错
所以cout 输出字符串的时候是把字符串看做char*插入到流中的吗,string不行吗

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

class Date
{
    int day,month,year;
    string s;
public:
    Date()
    {

    }
    Date(int pday,int pmonth,int pyear)
    {
      day = pday;
      month = pmonth;
      year = pyear;
    }
    Date(Date& d)
    {
      day = d.day;
      month = d.month;
      year = d.year;
    }
    ~Date()
    {

    }
    /*operator string()
    {
      ostringstream p;
      p << year << "," << month << "," << day;
      s = p.str();
      return s;
    }*/
    operator const char*()
    {
      ostringstream p;
      p << year << "," << month << "," << day;
      return p.str().c_str();
    }
};

int main()
{
    Date d(29,2,2004);

    cout << d << endl;

    return 0;
}

人造人 发表于 2023-1-22 21:59:23

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

class A {};

ostream &operator<<(ostream &os, const A &a) {
    return os;
}

class Date {
    int day, month, year;
    string s;
public:
    Date(int pday, int pmonth, int pyear) {
      day = pday;
      month = pmonth;
      year = pyear;
    }
    Date(Date &d) {
      day = d.day;
      month = d.month;
      year = d.year;
    }
    /*
    operator const string() {
      ostringstream p;
      p << year << "," << month << "," << day;
      s = p.str();
      return s;
    }
    */
    /*
    operator const char* ()
    {
      ostringstream p;
      p << year << "," << month << "," << day;
      return p.str().c_str();
    }
    */
    operator const A() {
      return A();
    }
};

int main() {
    Date d(29, 2, 2004);
    cout << d << endl;
    return 0;
}


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

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

class A {};

ostream &operator<<(ostream &os, const A &a) {
    return os;
}

class Date {
    int day, month, year;
    string s;
public:
    Date(int pday, int pmonth, int pyear) {
      day = pday;
      month = pmonth;
      year = pyear;
    }
    Date(Date &d) {
      day = d.day;
      month = d.month;
      year = d.year;
    }
    operator const string() {
      ostringstream p;
      p << year << "," << month << "," << day;
      s = p.str();
      return s;
    }
    /*
    operator const char* ()
    {
      ostringstream p;
      p << year << "," << month << "," << day;
      return p.str().c_str();
    }
    */
    /*
    operator const A() {
      return A();
    }
    */
};

ostream &operator<<(ostream &os, const string &s) {
    return os;
}

int main() {
    Date d(29, 2, 2004);
    cout << d << endl;
    return 0;
}


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

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

class A {};

ostream &operator<<(ostream &os, const A &a) {
    return os;
}

class Date {
    int day, month, year;
    string s;
public:
    Date(int pday, int pmonth, int pyear) {
      day = pday;
      month = pmonth;
      year = pyear;
    }
    Date(Date &d) {
      day = d.day;
      month = d.month;
      year = d.year;
    }
    operator const string() {
      ostringstream p;
      p << year << "," << month << "," << day;
      s = p.str();
      return s;
    }
    /*
    operator const char* ()
    {
      ostringstream p;
      p << year << "," << month << "," << day;
      return p.str().c_str();
    }
    */
    /*
    operator const A() {
      return A();
    }
    */
};

int main() {
    Date d(29, 2, 2004);
    cout << d << endl;
    return 0;
}

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

人造人 发表于 2023-1-22 22:35:57

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

class Date
{
    int day,month,year;
    string s;
public:
    Date()
    {

    }
    Date(int pday,int pmonth,int pyear)
    {
      day = pday;
      month = pmonth;
      year = pyear;
    }
    Date(Date& d)
    {
      day = d.day;
      month = d.month;
      year = d.year;
    }
    ~Date()
    {

    }
    operator string()
    {
      ostringstream p;
      p << year << "," << month << "," << day;
      s = p.str();
      return s;
    }
    /*
    operator const char*()
    {
      ostringstream p;
      p << year << "," << month << "," << day;
      return p.str().c_str();
    }
    */
};

int main()
{
    Date d(29,2,2004);

    cout << (string)d << endl;

    return 0;
}

一减一 发表于 2023-1-22 23:01:27

人造人 发表于 2023-1-22 22:35


我想问一下为啥char*不用这样写,string要这样写

dolly_yos2 发表于 2023-1-23 10:03:28

不是回答问题,只是好奇为什么不重载运算符(<<)而是自定义类型转换来实现这个效果?
页: [1]
查看完整版本: c++ 转换运算符问题