Musterklassen für C++
(Siehe auch 721242.)
»pair«
Das folgende Beispiel basiert auf einem Beispiel von Herb Sutter, wurde aber dann verändert und erweitert.
Die Zeile mit „gcc“ kann entfernt/ignoriert werden.
main.cpp
#include <iostream>
#include <ostream>#include <string>
class pair
{ int x, y;
public:
explicit pair( int const x = 0, int const y = 0 ): x{ x }, y{ y } {}
pair & operator ++() { ++x; return *this; }
pair operator ++( int ) { auto result = *this; ++*this; return result; }
pair & operator +=( const pair & o ){ x += o.x; y += o.y; return *this; }
::std::ostream & print( ::std::ostream & stream )const
{ return stream << '<' << x << ", " << y << '>'; }bool equals( const pair & other )const noexcept
{ return x == other.x && y == other.y; } };inline pair operator +( pair result, pair const & other )
{ result += other; return result; }inline ::std::ostream& operator <<( ::std::ostream & stream, pair const & object )
{ return object.print( stream ); }inline bool operator==( const pair & left, const pair & right )noexcept
{ return left.equals( right ); }bool operator==( const pair & left, const pair & right )noexcept __attribute__ ((pure)); /* gcc */
int main(){ pair const c; ::std::cout << c << '\n'; }
- Protokoll
<0, 0>
»operator ==« kann außerhalb des Klassenspezifizierers deklariert werden, wenn dies möglich ist, ohne auf private Einträge der Klasse zuzugreifen oder extra dafür neue öffentliche Einträge zum Klassenspezifizierer hinzuzufügen, andernfalls wird »operator ==« innerhalb des Klassenspezifizierers deklariert.
Protokoll
<0, 0>
F:\r\j\autocomp\main.cpp:10:28: warning: constructor parameter 'x' shadows the field 'x' of 'pair' [clang-diagnostic-shadow-field-in-constructor]
explicit pair( int const x = 0, int const y = 0 ): x{ x }, y{ y } {}
^
F:\r\j\autocomp\main.cpp:6:7: note: previous declaration is here
{ int x, y;
^
F:\r\j\autocomp\main.cpp:10:45: warning: constructor parameter 'y' shadows the field 'y' of 'pair' [clang-diagnostic-shadow-field-in-constructor]
explicit pair( int const x = 0, int const y = 0 ): x{ x }, y{ y } {}
^
F:\r\j\autocomp\main.cpp:6:10: note: previous declaration is here
{ int x, y;
^
Nach Bjarne Stroustrup
main.cpp
#include <vector>
template<typename T>
class Vec : public ::std::vector<T>
{ public:
using ::std::vector<T>::vector; // use the constructors from vector (under the name Vec)
T& operator[](int i) // range check
{ return ::std::vector<T>::at(i); }
const T& operator[](int i) const // range check const objects
{ return ::std::vector<T>::at(i); }};
int main(){ }
Protokoll
Nach einem Normentwurf
§ 3.1 34
main.cpp
#include <string>
struct C
{ std::string s;
C() : s() { }
C(const C& x): s(x.s) { }
C(C&& x): s(static_cast<::std::string&&>(x.s)) { }
// : s(std::move(x.s)) { }
C& operator=(const C& x) { s = x.s; return *this; }
C& operator=(C&& x) { s = static_cast<::std::string&&>(x.s); return *this; }
// { s = std::move(x.s); return *this; }
~C() { } };
int main(){ }
Protokoll
Tabelle
Die folgende Tabelle paßt nicht in diese Lektion. Sie wird vorübergehend hier aufbewahrt.
- Tabelle
.------------------------ compiler implicitly declares ------------------------.
.- User default copy destructor copy move move
| declares: constructor constructor assignment constructor assignment
|
|
|
| Nothing defaulted defaulted defaulted defaulted defaulted defaulted
|
| default user defaulted defaulted defaulted defaulted defaulted
| constructor declared
|
| copy not user defaulted defaulted* not not
| constructor declared declared declared declared
|
| Any not defaulted defaulted defaulted defaulted defaulted
| constructor declared
|
| destructor defaulted defaulted* user defaulted* not not
| declared declared declared
|
| copy defaulted defaulted* defaulted user not not
| assignment declared declared declared
|
| move not deleted defaulted deleted user not
| constructor declared declared declared
|
| move defaulted deleted defaulted deleted not user
| assignment declared declared
'-
* = deprecated