0

I need an explanation of Auxiliary operators. I understand that they are not members of a
given class. But how do you use them? Particularly in overloading operators situations.

2
Contributors
1
Reply
2
Views
12 Years
Discussion Span
Last Post by Narue
0

>But how do you use them? Particularly in overloading operators situations.
All of the friend functions in the following code are auxiliary functions.

#include <iostream>

using namespace std;

class Integer {
public:
  Integer(int init): data(init) {}
public:
  friend ostream& operator<<(ostream& out, const Integer& i);
  friend istream& operator>>(istream& in, Integer& i);
  friend Integer operator+(const Integer& a, const Integer& b);
private:
  int data;
};

int main()
{
  Integer a(10);
  Integer b(5);

  cout<< a + b <<endl;
}

ostream& operator<<(ostream& out, const Integer& i)
{
  return out<< i.data;
}

istream& operator>>(istream& in, Integer& i)
{
  return in>> i.data;
}

Integer operator+(const Integer& a, const Integer& b)
{
  return Integer(a.data + b.data);
}

Auxiliary functions are basically functions that don't need to be member functions (use your own judgement). Sometimes they need access to private members and should be friends, other times they can be written using the public interface of a class.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.