超载COUT查看指针内部的内容
Overloading cout to see what is inside pointer
在过去的一个月我正在研究很多C ,并且知道我也尝试练习一点,为此我克隆了电子项目。
在文件src/electron/atom/browser/api/atom_api_session.cc
中有此代码:
379 void Session::OnDownloadCreated(content::DownloadManager* manager,
380 content::DownloadItem* item) {
381 if (item->IsSavePackageDownload())
382 return;
383
384 v8::Locker locker(isolate());
385 v8::HandleScope handle_scope(isolate());
386 bool prevent_default = Emit(
387 "will-download",
388 DownloadItem::Create(isolate(), item),
389 item->GetWebContents());
390 if (prevent_default) {
391 item->Cancel(true);
392 item->Remove();
393 }
394 }
我想打印使用stdout::cout << manager;
的经理,因此我创建了一个函数,以使<<
超载,因为这些书告诉我:
375 void operator << (std::ostream & o, const content::DownloadManager* manager) {
376 o << "manager:" << manager;
377 }
但这根本不起作用。
content::DownloadManager*
看起来像是类实例的指针。简单地执行o << "manager:" << manager;
不会打印全类内容。您必须知道content::DownloadManager
的数据成员才能使用<<
操作员过载来打印它们。但是,在更新之后,您将必须使用return
std::ostream
。例如,查看以下示例,以使<<
>>
运算符。
struct Quote
{
std::string ticker;
std::string exchange;
std::string full_name;
double value;
std::string data;
long long timestampus;
long long timestamps;
friend std::ostream& operator<< (std::ostream& out, Quote& object)
{
out << object.ticker << " " << object.exchange << " " << object.full_name << " " << object.value << " " << object.data << " " << object.timestampus << " " << object.timestamps;
return out;
}
friend std::istream& operator>> (std::istream& in, Quote& object)
{
in >> object.ticker;
in >> object.exchange;
in >> object.full_name;
in >> object.value;
in >> object.data;
in >> object.timestampus;
in >> object.timestamps;
return in;
}
};
还要查看此链接,以了解您是否想将其定义为会员,朋友还是非朋友。希望这会有所帮助。
相关文章:
- 指针没有更新它在void函数内部指向的值
- 为什么向量内部的指针在从函数返回时会发生变化?
- 如何在类内部使用和访问静态指针到成员值s_ptm?
- 正确的方法是什么?调用指针到指针到指针内部的函数?
- 静态指针指向这个为mock for googletest定义的类内部
- 指针(指向指针)和新/malloc 向量(向量内部)c++
- 如何让指针的深层副本将内部结构发送到内核?
- 指向C++中成员的指针如何在内部工作?
- 作为 int 传递和类内部指针之间的差异给我带来了分段错误
- 指针帮助:取消指向访问其内部数据的结构的指针
- 在函数内部错误定位后,从函数中释放返回的指针会导致芯片崩溃
- 如果访问对象,则指向的对象内部的指针会更改内存位置
- 函数指针仅在 main 内部工作
- 如何将功能指针传递到该功能内部的函数
- 当指针作为指向另一个功能内部指针的指针传递时会发生什么
- 获取指向自我内部班级的指针
- 为具有内部指针的对象实现swap()
- 通过内部指针隐藏实现详细信息
- 指向另一个结构的内部指针的结构指针
- 标准::移动 - 标准::内部指针