1

I am writing my own integer class that can handle integers of any size. So far, I have overloaded the following operators successfully: =, +, -, *, /, %, >, <, >=, <=, ==, !=, +=, -=, *=, /=, and %=.

Now, I'm trying to overload the << operator to mimic the behavior of int's in the following lines of code:

int a = 5;
std::cout << a;

I have been looking at how to do this for a while, and so far all I've found is this:

std::ostream& operator<<(std::ostream& os, const T& obj)
{
  // Write obj to stream
  return os;
}

But this seems to be for if I wanted to stream something into my object (that is, having the << on the right side of my object). But I want to change the behavior of << when it is on the LEFT side of my object.

How can I set up the operator<< function to allow me to stream data into cout (or any other ostream)?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
xcdemon05
  • 678
  • 3
  • 7
  • 20
  • 1
    Just like you have it. That is the right operator to overload (assuming it is not a member of your class.) – juanchopanza May 03 '14 at 16:08
  • "But this seems to be for if I wanted to stream something into my object. (i.e. having the << on the right side of my object)." How do you figure that? The first operand (`ostream`, e.g. `cout`) is the LHS of the `<<` expression. –  May 03 '14 at 16:08
  • You've got it right, that's when it's on the left side. it's operator<<(left side, right side) – dutt May 03 '14 at 16:08
  • But when I do that, I get the error "too many parameters for this operator function" – xcdemon05 May 03 '14 at 16:10
  • 4
    @xcdemon05 OK, you probably declared it as a member. It should be a non-member. – juanchopanza May 03 '14 at 16:11
  • 2
    @xcdemon05 Like juanchopanza said, it should be a non-member function, but note that you can still define it directly inside your class by making it a `friend` non-member function. –  May 03 '14 at 16:12
  • possible duplicate of [Operator overloading](http://stackoverflow.com/questions/4421706/operator-overloading) – danielschemmel May 03 '14 at 17:16
  • @LaszloPapp sorry haha i've been out of the house ;) – xcdemon05 May 04 '14 at 02:59

2 Answers2

2

How can I set up the operator<< function to allow me to stream data into cout (or any other ostream)?

The way you did here:

std::ostream& operator<<(std::ostream& os, const T& obj)
{
  // write obj to stream
  return os;
}

As others usefully pointed out in the comments, you will need to place it outside of your integer classes to be a "free function". It could still be the member of your integer classes, but yeah, that is it.

But this seems to be for if I wanted to stream something into my object. (i.e. having the << on the right side of my object). But I want to change the behavior of << when it is on the LEFT side of my object.

I am not sure where you got that from but the first argument is the left side argument which is the output stream in your case, and the second is the instance of your integer class that gets written to that output stream.

László Papp
  • 51,870
  • 39
  • 111
  • 135
1

In C++ streams,

std::cout << a;

is just syntactic sugar for

operator<<(std::cout, a);

which matches the signature

std::ostream& operator<<(std::ostream& os, const int &a);

So likewise,

std::cout << myBigInt;

would be syntactic sugar for

operator<<(cstd::cout, myBigInt);

Therefore the snippet you posted enables the syntax

os << obj; // os is an ostream; obj is a T

In other words, the << is on the left side of obj and/or myBigInt since the binary operator goes in between the two operands that you see in the function argument list.

Nicu Stiurca
  • 8,747
  • 8
  • 40
  • 48