C ++中的变量转储 [英] Variable Dumping in C++

查看:114
本文介绍了C ++中的变量转储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能对复杂的结构或甚至是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?

我想在PHP中类似于print_r()或var_dump()。

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

干杯,
-Fin

Cheers, -Fin

推荐答案

简单的答案是:不,除非你自己手动写这样的例程。

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

这通常不是一个坏主意,但它有常见的问题,如果你经常添加/更改成员与类不同步。这是不可避免的,因为C ++缺少任何形式的自省到 struct

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.

如果你决定这样,最好的办法是为 std :: ostream&运算符<<<(std :: ostream& os,MyClass const&),这将允许您的类打印到任何IOStreams输出流。

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.

这篇关于C ++中的变量转储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆