Answer the question
In order to leave comments, you need to log in
How to cast to default string?
#include <iostream>
#include <string>
class A {
private:
std::string s = std::string("hello");
public:
operator std::string& () {return s;}
//operator const char* () {return s.c_str();}
};
int main() {
A a;
std::cout << a << '\n';
}
Answer the question
In order to leave comments, you need to log in
Need to explicitly convert for the cout operator
#include <iostream>
#include <string>
class A {
private:
std::string s = std::string("hello");
public:
operator std::string& () {return s;}
//operator const char* () {return s.c_str();}
};
int main() {
A a;
std::cout << (std::string)a << '\n';
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question