鱼C论坛

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

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

[复制链接]
发表于 2023-1-22 21:59:22 | 显示全部楼层 |阅读模式
10鱼币
我想能直接输出类,但是,如果返回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);

最佳答案

查看完整内容

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

使用道具 举报

发表于 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);
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 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;
}
想知道小甲鱼最近在做啥?请访问 -> 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-11-17 17:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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