Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- printf("%s", obj) //prints "some data"
- #include <cstdio>
- #include <list>
- #include <string>
- class Example{
- public:
- operator int() const{
- return 5;
- }
- operator const char*() const{
- return m_text;
- }
- operator std::list<std::string>() const{
- return m_lines;
- }
- private:
- const char* m_text = "5";
- const std::list<std::string> m_lines = {"1", "2", "3"};
- };
- void func(int d){
- std::printf("%dn", d);
- }
- /*
- А вот так уже нельзя, неоднозначность, компилятор не будет знать что вы от него хотите
- void func(const std::list<std::string>& l){
- for (const auto& line:l){
- printf("%sn", line.c_str());
- }
- }
- */
- void funcList(const std::list<std::string>& l){
- for (const auto& line:l){
- printf("%sn", line.c_str());
- }
- }
- int main()
- {
- Example e;
- std::printf("%dn", (int)e);
- std::printf("%sn", (const char*)e);
- func(e);
- funcList(e);
- return 0;
- }
Add Comment
Please, Sign In to add comment