Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Overloading

 

Overloading is to redefine a standard operator. The example below shows how to overload operator<<.

 

#include <iostream>

struct MyClass
{
  MyClass(const int value) : mValue(value) {}
  private:
  const int mValue;
  friend std::ostream& operator<<(std::ostream& os, const MyClass& myClass);
  friend std::ostream& operator<<(std::ostream& os, const MyClass& myClass);
};

std::ostream& operator<<(std::ostream& os, const MyClass& myClass)
{
  os << "MyClass.value: " << myClass.mValue;
  return os;
}

int main()
{
  const MyClass myClass(13);
  std::cout << myClass << '\n';
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict