overload the << and >> operators and declare them as friend your class, and then use them. Here is a sample code.
#include <iostream> #include <string> using namespace std; class Map { friend Map& operator << (Map &map, string str); friend Map& operator >> (Map &map, string str); }; Map& operator << (Map &map, string str) { //do work, save the map with name str cout << "Saving into \""<< str << "\"" << endl; return map; } Map& operator >> (Map &map, string str) { // do work, load the map named str into map cout << "Loading from \"" << str << "\"" << endl; return map; } int main (void) { Map map; string str; map << "name1"; map >> "name2"; }
Note that for your purpose, the interpretation of the return of the object is up to you, because obj << "hello" << "hi"; can mean loading obj from "hello" and "hello"? or add them in this order, it is up to you. Also obj >> "hello" >> "hi"; may mean saving obj in two files named hello and hello
source share