Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) OperatorStreamOutExample1

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppOperatorStreamOutExample1/CppOperatorStreamOutExample1.pro

 

TEMPLATE = app
CONFIG += console
CONFIG -= qt
QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++ -Werror
SOURCES += main.cpp

 

 

 

 

 

./CppOperatorStreamOutExample1/main.cpp

 

#include <iostream>

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

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

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

/* Screen output

MyClass.value: 13
Press <RETURN> to close this window...

*/

 

 

 

 

 

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

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml