Musterklassen für C++ (Musterklassen für C++), Lektion, Seite 723768
https://www.purl.org/stefan_ram/pub/musterklassen_c++ (Permalink) ist die kanonische URI dieser Seite.
Stefan Ram
C++-Kurs

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

 

Seiteninformationen und Impressum   |   Mitteilungsformular  |   "ram@zedat.fu-berlin.de" (ohne die Anführungszeichen) ist die Netzpostadresse von Stefan Ram.   |   Eine Verbindung zur Stefan-Ram-Startseite befindet sich oben auf dieser Seite hinter dem Text "Stefan Ram".)  |   Der Urheber dieses Textes ist Stefan Ram. Alle Rechte sind vorbehalten. Diese Seite ist eine Veröffentlichung von Stefan Ram. Schlüsselwörter zu dieser Seite/relevant keywords describing this page: Stefan Ram Berlin slrprd slrprd stefanramberlin spellched stefanram723768 stefan_ram:723768 Musterklassen für C++ Stefan Ram, Berlin, and, or, near, uni, online, slrprd, slrprdqxx, slrprddoc, slrprd723768, slrprddef723768, PbclevtugFgrsnaEnz Erklärung, Beschreibung, Info, Information, Hinweis,

Der Urheber dieses Textes ist Stefan Ram. Alle Rechte sind vorbehalten. Diese Seite ist eine Veröffentlichung von Stefan Ram.
https://www.purl.org/stefan_ram/pub/musterklassen_c++