重载cout以查看指针内部是什么

时间:2017-09-27 02:39:18

标签: c++ overloading

在过去的一个月里,我正在学习很多C ++,并知道我正在努力练习一点,为此克隆了Electron项目。

在文件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 }  

但这根本不起作用。

1 个答案:

答案 0 :(得分:0)

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;
  }
};

另请查看此How to get the anchor from the URL using jQuery?以了解您是要将其定义为成员,朋友还是非朋友。希望这会有所帮助。