Can I print an object of the class using printf()?
Asked Answered
E

3

7

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;
}
Ebby answered 15/2, 2016 at 16:28 Comment(6)
Please remove the c tag.Recently
@IanAbbott Here you are!Ebby
What happened when you tried printf?Budgerigar
@Budgerigar Compilation was stopped, because expression "%o" is not valid. When I tried a valid one, like "%d", there was an error expects argument of type int.Ebby
So there's the answer to your question.Budgerigar
another way is to add a toString() method, which you decide how to format your objects output. Then call the method from the printf arguments eg printf("my obj: %s\n",myObj.toString().c_str())Stilla
O
7

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&.

Oistrakh answered 15/2, 2016 at 16:31 Comment(0)
R
0

You can however, use a custom print function in your class, that would print your object the way you want:

...
void print() const {
  printf("[%d, %d]", x, y);
}
...

int main()
{
  Point point;
  point.print();
  return 0;
}

You can also use fprintf and a custom stream if you want to. This is not entirely an answer to your question, but seems to be useful in the described situation.

Redemption answered 15/2, 2016 at 17:16 Comment(2)
It's true, but using std::cout is more "natural", I guess.Ebby
@Nathiss, it depends. For example google style sheet forbids using streams and actually encourages to use fprintf instead. UPD: uh.. oh... not any more. Now they slightly discourage using them, but do not forbid anymore.Redemption
M
0

You must have to overload the operator otherwise , the printf() will get confused what to print of the object point as the class has two members.

Maffick answered 15/2, 2016 at 17:19 Comment(4)
Or you can declare another getter() function which can print the values of the members.Maffick
How can I overload printf() function for that class?Ebby
printf() itself looks like overloading. Because, in this you can have printf("%d", aDec) and printf("%f", aFloat). This looks a lot like function overloading , because we are using the same function name, but the function is accepting different parameter types – which is one of the ways we can overload functions. But for this class i would suggest you to do overloading <<Maffick
From what I know, printf() is C function. In standard C we can not overload any function. We can print int and float variables, because in function printf() list of arguments is not specified. Because of this printf() can print any of basic types(int, float, double, char, bool) but not types specified by user.Ebby

© 2022 - 2024 — McMap. All rights reserved.