超载COUT查看指针内部的内容

Overloading cout to see what is inside pointer

本文关键字:内部 指针 COUT 超载      更新时间:2023-10-16

在过去的一个月我正在研究很多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;
  }
};

还要查看此链接,以了解您是否想将其定义为会员,朋友还是非朋友。希望这会有所帮助。