I have some code like this:
class Point {
public:
int x,y;
Point() : x(1), y(1) {}
}
Can I print object of that class using printf()
:
int main()
{
Point point;
printf("%o",point);
return 0;
}
or I have to overload operator<<
and use std::cout
:
std::ostream& operator<<(std::ostream& os, Point const& p)
{
os << p.x << "," << p.y;
return os;
}
int main()
{
Point point;
std::cout << point;
return 0;
}
printf
? – Budgerigar"%o"
is not valid. When I tried a valid one, like"%d"
, there was an error expects argument of type int. – EbbytoString()
method, which you decide how to format your objects output. Then call the method from theprintf
arguments egprintf("my obj: %s\n",myObj.toString().c_str())
– Stilla