0

I am very new to C++ operator overloading and having some teething trouble.

I have defined:

void Graph::operator>>(const char* param)

The above function had to accept a string as input and then perform certain actions on the string. How do I call this function ? In what ways can I use it?

0

4 Answers 4

3

If you want to define operator so that you can do:

cin >> myGraph cout << myGraph 

You need to do something like this example below:

struct Entry { string symbol; string original; string currency; Entry() {} ~Entry() {} Entry(string& symbol, string& original, string& currency) : symbol(symbol), original(original), currency(currency) {} Entry(const Entry& e) : symbol(e.symbol), original(e.original), currency(e.currency) {} }; istream& operator>>(istream& is, Entry& en); ostream& operator<<(ostream& os, const Entry& en); 

Then implement operators:

istream& operator>>(istream& is, Entry& en) { is >> en.symbol; is >> en.original; is >> en.currency; return is; } ostream& operator<<(ostream& os, const Entry& en) { os << en.symbol << " " << en.original << " " << en.currency; return os; } 

Note: in this case the Entry is struct so it's members are public. If you don't want to make them public you can define the operator methods as friends so that they can access private members of Entry.

Here is how Entry would look like if it's members were not public:

struct Entry { private: string symbol; string original; string currency; public: Entry() {} ~Entry() {} Entry(string& symbol, string& original, string& currency) : symbol(symbol), original(original), currency(currency) {} Entry(const Entry& e) : symbol(e.symbol), original(e.original), currency(e.currency) {} friend istream& operator>>(istream& is, Entry& en); friend ostream& operator<<(ostream& os, const Entry& en); }; 
Sign up to request clarification or add additional context in comments.

1 Comment

Correct use of >> but does not really answer the question.
2
Graph myGraph; myGraph >> "bla"; 

Note that yours is a weird use of operator>>(). Normally it's used like this:

NumericObject NumericObject::operator>>(NumericObject const& operand) const; // bitshifts to the right 

and

std::istream& operator>>(istream& in, StreamableObject& obj); // parses a string representation of obj 

Comments

1

I'm guessing what you are actually trying to do is be able to write code like this:

cin >> myGraph; cout << myGraph; 

Note that the graph object is not actually the object that gets its method called.

In this case, what you actually want to do is overload the global operator>> functions:

std::istream& operator>> (std::istream&, graph&); std::ostream& operator<< (std::ostream&, const graph&); 

Comments

0

If you're new to this, you picked a rather interesting (read as "not simple") problem to try to solve.

The operator overloads are not exactly functions. They are called indirectly when the compiler attempts to resolve code that looks something like this:

Graph g = new Graph(); g >> "do something"; 

I recommend you do NOT do this. Operator overloading can lead to VERY difficult bugs to troubleshoot, thanks to side effects. They are also hard on anyone who has to maintain your code (which might be you after you forgot why you did this).

Use operator overloads only when the meaning and use of them is intuitive.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.