从指针调用重载运算符

don*_*ald 2 c++ operator-overloading

我有一个简单的 C++ 测试类,它char * operator()工作正常。问题是,当我在堆上创建它时,我无法使用它。

test t;
printf(t);
Run Code Online (Sandbox Code Playgroud)

可以,但是

test *t=new test();
printf(t);
Run Code Online (Sandbox Code Playgroud)

不是。除了 printf(*t) 之外还有什么方法可以解决吗?

fre*_*low 5

隐式转换为char*可能听起来是个好主意,但相信我,事实并非如此。猜猜为什么std::string没有一个。只需编写一个c_str方法并调用printf("%s", t->c_str()). 或者甚至更好,operator<<为您的课程超载。如果您向我们展示课程,我们可以帮助您。