简体   繁体   中英

Overload << and >> to work well with pointers C++

I am trying to add the these functions:

friend std::ostream& operator <<( std::ostream& outs, const TrashCan * drive );
friend std::istream& operator >>( std::istream& ins, TrashCan * & drive );

So it works properly with this driver code in main:

#include <iostream>
#include <stdexcept>
#include "TrashCan.h"


int main( ) {

using namespace std;

cout << "Welcome to my TrashCan Program!" << endl;

TrashCan myCan;
TrashCan yourCan;
TrashCan empty( 0, 0 );
TrashCan * ptrCan = new TrashCan( 0, 0 ); 
TrashCan * nullCan = NULL; 

// using overloaded operations for pointers...
cin >> ptrCan;
cin >> nullCan;  // 

cout << ptrCan << endl;
cout << nullCan << endl;  //

My Trashcan class constructor is as such:

TrashCan( int size, int contents );

I am familiar with overloading simple operations like + - and ≤ but am quite confused when it comes to overloading iostream functions so that they work with pointers. Any ideas?

Although it is possible to just overload the input and output operators for pointers, I don't consider it to be a good idea in the first place: in C++ there is very little need to use pointers and there is no point in encouraging users to use operators. That is, the simple solution is: don't use pointers!

Conceptually, there is nothing different between overloading input or output operators for pointers compared to other types. That is, you'd just overload

std::ostream& operator<< (std::ostream& out, TrashCan const* drive) {
    return *drive;
}
std::istream& operator>> (std::istream& in, TrashCan* drive) {
    return *drive;
}

Both of the implementations just delegate to the corresponding reference version reducing the problem to a problem with a known solution (I/O with references to objects).

I personally think it makes more sense to overload for TrashCan objects, and then dereference pointers at the place where you need to print the objects.

Without knowing much about your problem, I will propose that you need only one signature.

// Take const reference, not pointer
friend std::ostream& operator <<( std::ostream& outs, const TrashCan& drive );


TrashCan * ptrCan = new TrashCan( 0, 0 ); 
// [...]
std::cout << *ptrCan << endl;    // dereference.  no copies made.

Another benefit of having one function definition is that the code will be more maintainable, ie you won't have two overloads of a function that need to be maintained in parallel, or refactored to use the same code.

You shouldn't overload operators for pointers. You should pass an object or a reference to that object.

They should declare like this:

std::ostream& operator <<(std::ostream& out, const TrashCan & drive );
std::istream& operator >>(std::istream& in, TrashCan  & drive );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM