0

This past month I am studying a lot of C++, and know I'm trying too practice a little, and for that I cloned the Electron project.

In the file src/electron/atom/browser/api/atom_api_session.cc there is this code:

  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 }

I would like to print what is the manager with stdout::cout << manager;, so I created a function to overload the << as the books told me:

  375 void operator << (std::ostream & o, const content::DownloadManager* manager) {                                                           
  376   o << "manager:" << manager;                                                                                                            
  377 }  

But this isn't working at all.

  • 1
    you should return a stream, find some examples. – perreal Sep 27 '17 at 02:45
  • "this isn't working at all" - how is it not working? What happens and what is the expected output? – Jay Sep 27 '17 at 02:48
  • @jay The compiler fails –  Sep 27 '17 at 02:49
  • The return type should be `std::ostream&` (reference important, it has to be the same `ostream`) and at the end of the function you should return `o`. If you don't do that the next call to `operator<<` will have no stream to work with. – Paul Rooney Sep 27 '17 at 02:57

1 Answers1

0

The content::DownloadManager* looks like a pointer to a class instance. Simply doing o << "manager:" << manager; will not print the content of the class. You have to know the data members of content::DownloadManager to print them using <<operator overloading. However you will have to return std::ostream after updating it. For example look at the following example for overloading the << >> operators.

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

Also take a look this link to understand whether you wanna define that as a member or friend or non-friend. Hope this helps.

Panch
  • 1,097
  • 3
  • 12
  • 43