Operatorüberladung in C++
Ausdruck als Elementfunktion als freie Funktion
@a (a).operator@() operator@(a)
a@b (a).operator@(b) operator@(a,b)
a=b (a).operator=(b)
a[b] (a).operator[](b)
a-> (a).operator->()
a@ (a).operator@(0) operator@(a,0)
main.cpp
#include <initializer_list>
#include <ostream>
#include <iostream>
#include <vector>using namespace ::std::literals;
template< class I >
static void print
( ::std::ostream & ostream, I const begin, I const top )
{ if( begin != top )
{ auto p = begin;
while( true )
{ ostream << *p++;
if( p == top )break;
ostream << ", "s; }}}template< class C >
static inline ::std::ostream & operator <<
( ::std::ostream & ostream, ::std::vector< C > const & vector )
{ ::print( ostream, cbegin( vector ), cend( vector ));
return ostream; }int main()
{ ::std::vector< int >v = { 27, 28, 29 };
::std::cout << v << '\n'; }transcript
27, 28, 29
---