|
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;
- }
复制代码
- #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
|