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;
}
Can I print object of that class using
printf()
?
No. printf
is not extensible in that sense.
Your best option is to overload operator<<
between std::ostream
and Point
.
PS I suggest changing the argument type to Point const&
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With