C ++ overload operator '>>'

I looked through many different examples and explanations, but no one answered what I was looking for. I have three classes with the connect method for each of them:

class foo { ... }
void foo::connect(bar br) { ... }

class bar { ... }
bar& bar::connect(baz bz) { ... }

class baz { ... }
baz& baz::connect() { ... }

In my main class, I plug them in as follows:

foo.connect(bar);
bar.connect(baz);
baz.connect();

or

foo.connect( bar.connect( baz.connect() ) );

(I know this is briefly explained, I can explain it better if necessary)

So, I tried to overload the "→" operator to have something like this in the main function:

foo >> bar >> baz;

It works for the first statement, so if I just do the following, it works fine:

foo >> bar.connect(baz.connect);

But, when I set another operator "→", g ++ returns this error:

error: no match foroperator>>’ in ‘operator>>((* & foo), (* & bar)) >> baz.baz::connect()’

I think that I am not overloading the "→" operator correctly:

bar& operator>> (bar &br, baz &bz)
{
  ...
}

Thanks for the help:)

+4
3

- : , .

foo >> bar >> baz;

[ ]

operator>> (operator>> (foo, bar), baz);

bar& operator>> (foo& f, bar& b) {
    f.connect(b);
    return b;
}
bar& operator>> (bar& b0, baz& b1) {
    return b0.connect(b1);
}

, connect(), , operator>>(), .

+5

, , , . :

foo& operator>>(foo&, bar&) { ... }

, :

foo >> bar >> baz;

, foo >> bar bar ( foo), :

??? operator>>(bar&, baz&) { ... }

, , baz&, .

, ( foo >> bar foo), operator>> ( ) :

??? operator>>(foo&, baz&) { ... }
+2

bar& baz& , :

foo >> bar.connect(baz.connect);

foo& a bar& . operator>>?

, , , :

foo.connect(bar);
bar.connect(baz);
baz.connect();

NOT the same as:

foo.connect( bar.connect( baz.connect() ) );

The order of operations is different; in the second example it is executed first baz.connect(), then bar.connect()etc. This is the opposite of the first example. This may not matter in your application, but it is something to consider.

+2
source

All Articles