views:

288

answers:

4

Hey,

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

I've defined: 'void Graph::operator>>(const char* param)' to accept a string and then input then perform certain actions on the object.

How do I call this function that I've defined (>>) ? In what ways can I use it?

+2  A: 
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
A: 

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.

Alan McBee
+1  A: 

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&);
Tal Pressman
+2  A: 

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);
};
stefanB
Thanks for edit, I am not sure why I typed 'global' ...
stefanB
Correct use of >> but does not really answer the question.
Martin York