Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable Dumping in C++

Is it possible to do a 'dump' on complex structures or even arrays in C++, in order to visually be able to check out what they've got inside them?

I'm thinking of something similar to print_r() or var_dump() in PHP.

Cheers, -Fin

like image 508
Fintan Avatar asked Apr 11 '09 13:04

Fintan


1 Answers

The short answer is: no, unless you manually write such a routine yourself.

Which is often not a bad idea, but it has the usual problems of getting out of sync with the class if you are adding/changing members often. This is unavoidable because C++ lacks any form of introspection into structs.

If you decide to go that way, the best thing to do is to write an overload for std::ostream& operator<<(std::ostream& os, MyClass const&), which will enable your class to be printed to any IOStreams output stream.

like image 83
j_random_hacker Avatar answered Sep 22 '22 14:09

j_random_hacker