the overloading operators can have more than 2 parameters?

the overloading operators can have more than 2 parameters?
i'm thinking on 'operator >>' or 'operator <<', because it can be shift or using the ostream class...
can anyone explain more about it?
To answer the title question: No. The only ternary (as opposed to binary or unary) operator in C++ is the ?: operator ("conditional operator"), and that can't be overloaded in C++.

The >> and << operators are still binary operators. It's just that they return a reference to the left-hand-side stream, so you can chain the operations. Just like addition (3 + 2 + 1) is the same as ((3 + 2) + 1).
Last edited on
binary because the need 2 numbers(thinking on plus for example).... thank you so much for all.
the operator << can be binary or unary.... because can have 2 operators(the ostream(what call it) and the string(or the class name variable)).
now understand more about it. thank you so much for all
Last edited on
the operator << can be binary or unary
Technically, it's binary either way, not unary.

You can define it in a class, but it's still binary, it's just that the "this" parameter is implicitly the first parameter.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Example program
#include <iostream>

class Foo {
  public:
    // Still a binary operator!
    Foo& operator<<(int a)
    {
    	sum += a;
        return *this; // return reference to self to allow for operator chaining
    }
    
    int sum = 0;
};

int main()
{
	{
	    Foo foo;
	    foo << 3 << 2 << 1;
	    std::cout << foo.sum << '\n';
	}
	{
	    Foo foo2;
	    (((foo2 << 3) << 2) << 1); // equivalent
	    std::cout << foo2.sum << '\n';
	}
}

Last edited on
but it can be too(inside the class):
1
2
3
4
5
6
7
8
9
10
11
12
13
friend ostream & operator<<(ostream &os, vector<Errors> &err  )
        {
            for (unsigned int i=0; i<err.size(); i++)
            {
                os << "Message: " << err[i].Message << "\n";
                if(err[i].Suggestion!="")
                    os<< "Suggestion: " << err[i].Suggestion << "\n";
                os << "Line: " << err[i].Line << "\n" << "Column: " << err[i].Column << "\n";
                if(i<err.size()-1)
                    os << "\n";
            }
            return os;
        }

i need understand the ostream, in these case, calls the << operator
Yes, in that case you're defining it as friend though. So it's still binary, kinda like a static class function. Edit: bad analogy, it isn't static, see keskiverto's post.

ostream is just the base class of things like fstream and cout (cout is a global object). So it's printing those things to the output stream os, which could be a file, a stringstream, or standard out (cout). Does that answer your question?
Last edited on
the c++ book is limited on that information...

yes is answered.. thank you so much for all... thank you
but it can be too(inside the class):

No. A friend is not a member.

Verbosely:
1
2
3
4
5
6
7
8
9
10
11
12
class Foo; // forward declaration of a typename

void bar( const Foo& ); // function declaration

class Foo { // class definition
  friend void bar( const Foo& ); // declare friendship
};

void bar( const Foo& f )
{
  // function implementation
}

For obvious convenience reasons the above can be condensed into:
1
2
3
4
5
6
class Foo { // class definition
  friend void bar( const Foo& f ) // declare function and make if a friend
  {
    // function implementation
  }
};

Nevertheless, the bar is a standalone function that takes one parameter.


EDIT: a semicolon at line was a mistake.
Last edited on
thank you so much for all
@keskiverto,

wouldn't the condensed bar function have the semi-colon at the end of line 2 removed? Since it is both a function declaration and definition.
all function declaration have semi-colon at the end
Last edited on
This does NOT compile:
1
2
3
4
5
6
7
8
9
class Foo
{ // class definition
   friend void bar(const Foo& f);
   {
      // function implementation
   }
};

int main() {}

GCC gives this compile time error for line 4:
main.cpp|4|error: expected unqualified-id before '{' token

Visual Studio 2019 vomits up 3 errors for line 4:
Error (active) E0169 expected a declaration
Error C2059 syntax error: '{'
Error C2334 unexpected token(s) preceding '{'; skipping apparent function body

This compiles without a problem, not even a warning:
1
2
3
4
5
6
7
8
9
class Foo
{ // class definition
   friend void bar(const Foo& f)
   {
      // function implementation
   }
};

int main() {}

So....what is the difference between the two snippets?

The semi-colon at the end of line 3.
Sorry, I had a typo.

Beware the heinous power of copy-paste programming!
Copy-paste-oriented programming is my favorite paradigm!
Gano hehehehe
Sorry, I had a typo.

I thought you had, so I mentioned it. :)
Topic archived. No new replies allowed.