[ Web Proxy ]
URL:
Viewing: https://de.cppreference.com/cpp/language/as_operator [Back]  [Original]

Copy assignment operator cppreference.com
cppreference.com
Namensrume
Varianten

Copy assignment operator

Aus cppreference.com

<metanoindex/>

 
 
Sprache C++
Allgemeine Themen
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Flusskontrolle
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Bedingte Ausfhrung Aussagen
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Iterationsanweisungen
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Gehe Aussagen
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Funktionen
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Funktion Erklrung
Lambda-Funktion Erklrung
Funktions-Template
inline-Spezifizierer
Exception-Spezifikationen (veraltet)
noexcept Spezifizierer (C++11)
Ausnahmen
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Namespaces
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Types
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier (C++11)
Specifiers
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
cv Planer
Lagerdauer Planer
constexpr Spezifizierer (C++11)
auto Spezifizierer (C++11)
alignas Spezifizierer (C++11)
Initialisierung
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Literale
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Expressions
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
alternative Darstellungen
Utilities
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Types
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
Typ Aliasdeklaration (C++11)
Attribute (C++11)
Wirft
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
impliziten Konvertierungen
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-Stil und funktionale Besetzung
Speicherzuweisung
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Classes
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Class-spezifische Funktion Eigenschaften
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
explizit (C++11)
statisch
Besondere Member-Funktionen
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Templates
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Klassen-Template
Funktions-Template
Template-Spezialisierung
Parameter Packs (C++11)
Verschiedenes
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Inline Montage
 
Eine Kopie Zuweisungsoperator der Klasse T ist eine Non-Vorlage nicht-statische Member-Funktion mit dem Namen operator=, die genau einen Parameter vom Typ T, T&, const T&, volatile T& oder const volatile T& dauert. Ein Typ mit einer ffentlichen Kopie Zuweisungsoperator ist CopyAssignable .
Original:
A copy assignment operator of class T is a non-template non-static member function with the name operator= that takes exactly one parameter of type T, T&, const T&, volatile T&, or const volatile T&. A type with a public copy assignment operator is CopyAssignable.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Syntax

class_name & class_name :: operator= ( class_name ) (1) (seit C++11)
class_name & class_name :: operator= ( const class_name & ) (2) (seit C++11)
class_name & class_name :: operator= ( const class_name & ) = default; (3) (seit C++11)
class_name & class_name :: operator= ( const class_name & ) = delete; (4) (seit C++11)

Erklrung

# Typische Deklaration einer Zuweisungsoperator, wenn copy-and-swap idiom verwendet werden knnen
Original:
# Typical declaration of a copy assignment operator when copy-and-swap idiom can be used
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
# Typische Deklaration einer Zuweisungsoperator bei copy-and-Swap-Idiom nicht verwendet werden kann
Original:
# Typical declaration of a copy assignment operator when copy-and-swap idiom cannot be used
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
# Erzwingen einer Zuweisungsoperator vom Compiler erzeugt werden
Original:
# Forcing a copy assignment operator to be generated by the compiler
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
# Vermeiden impliziten Zuweisungsoperator
Original:
# Avoiding implicit copy assignment
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Der Zuweisungsoperator wird aufgerufen, wenn durch berlast Auflsung, zB ausgewhlte wenn ein Objekt auf der linken Seite einer Zuweisung Ausdruck .
Original:
The copy assignment operator is called whenever selected by berlast Auflsung, e.g. when an object appears on the left side of an assignment expression.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Implizit deklarierte Zuweisungsoperator

Wenn keine benutzerdefinierten Kopie Zuweisungsoperatoren fr eine Klasse-Typ (struct, class oder union) vorgesehen sind, wird der Compiler immer erklren, ein als Inline ffentlichen Member der Klasse. Diese implizit deklariert Zuweisungsoperator hat die Form T& T::operator=(const T&), wenn alle der folgenden Bedingungen erfllt ist:
Original:
If no user-defined copy assignment operators are provided for a class type (struct, class, or union), the compiler will always declare one as an inline public member of the class. This implicitly-declared copy assignment operator has the form T& T::operator=(const T&) if all of the following is true:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • Jede direkte Basis B der T hat einen Zuweisungsoperator, deren Parameter B oder const B& oder const volatile B&
    Original:
    each direct base B of T has a copy assignment operator whose parameters are B or const B& or const volatile B&
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • jede nicht-statische Daten Mitglied M der T der Klasse-Typ oder ein Array von Klasse-Typ hat eine Kopie Zuweisungsoperator deren Parameter M oder const M& oder const volatile M&
    Original:
    each non-static data member M of T of class type or array of class type has a copy assignment operator whose parameters are M or const M& or const volatile M&
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
Ansonsten ist die implizit deklariert Zuweisungsoperator als T& T::operator=(T&) erklrt. (Beachten Sie, dass aufgrund dieser Regeln, die implizit deklariert Zuweisungsoperator kann nicht auf einen flchtigen lvalue Argument binden)
Original:
Otherwise the implicitly-declared copy assignment operator is declared as T& T::operator=(T&). (Note that due to these rules, the implicitly-declared copy assignment operator cannot bind to a volatile lvalue argument)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Eine Klasse kann mehrere Kopien Zuweisungsoperatoren, zB sowohl T& T::operator=(const T&) und T& T::operator=(T). Wenn einige benutzerdefinierte Kopie Zuweisungsoperatoren vorhanden sind, kann der Benutzer noch zwingen die Erzeugung des implizit deklarierten Zuweisungsoperator mit dem Schlsselwort default .
Original:
A class can have multiple copy assignment operators, e.g. both T& T::operator=(const T&) and T& T::operator=(T). If some user-defined copy assignment operators are present, the user may still force the generation of the implicitly declared copy assignment operator with the keyword default.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Da der Zuweisungsoperator ist immer fr jede Klasse deklariert wird, wird die Basisklasse Zuweisungsoperator immer versteckt. Wenn eine using-Deklaration wird verwendet, um in der Zuweisungsoperator von der Basisklasse zu bringen, und das Argument type knnte das gleiche wie das Argument Typ des impliziten Zuweisungsoperator der abgeleiteten Klasse zu sein, wird die using-Deklaration auch von der impliziten versteckt Erklrung .
Original:
Because the copy assignment operator is always declared for any class, the base class assignment operator is always hidden. If a using-declaration is used to bring in the assignment operator from the base class, and its argument type could be the same as the argument type of the implicit assignment operator of the derived class, the using-declaration is also hidden by the implicit declaration.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Gelschte implizit deklariert Zuweisungsoperator

Die implizit deklariert oder ausgefallen Zuweisungsoperator fr die Klasse T ist definiert als gelscht in einer der folgenden Punkte zutrifft:
Original:
The implicitly-declared or defaulted copy assignment operator for class T is defined as deleted in any of the following is true:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • T einen nicht-statisches Datenelement, das const ist
    Original:
    T has a non-static data member that is const
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T hat eine nicht-statische Daten Mitglied einer Referenz-Typ .
    Original:
    T has a non-static data member of a reference type.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T hat eine nicht-statische Daten Mitglied, die nicht kopiergeschtzt zugeordnet werden knnen (gelscht hat, unzugnglich oder mehrdeutig Zuweisungsoperator)
    Original:
    T has a non-static data member that cannot be copy-assigned (has deleted, inaccessible, or ambiguous copy assignment operator)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T hat direkte oder virtuelle Basisklasse, die nicht kopiergeschtzt zugeordnet werden knnen (gelscht hat, unzugnglich oder mehrdeutig move Zuweisungsoperator)
    Original:
    T has direct or virtual base class that cannot be copy-assigned (has deleted, inaccessible, or ambiguous move assignment operator)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T verfgt ber eine benutzerfreundliche erklrt move Konstruktor
    Original:
    T has a user-declared move constructor
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T verfgt ber eine benutzerfreundliche erklrt move Zuweisungsoperator
    Original:
    T has a user-declared move assignment operator
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

Trivial Zuweisungsoperator

Die implizit deklariert Zuweisungsoperator fr die Klasse T ist trivial, wenn alle der folgenden Bedingungen erfllt ist:
Original:
The implicitly-declared copy assignment operator for class T is trivial if all of the following is true:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • T hat keine virtuelle Member-Funktionen
    Original:
    T has no virtual member functions
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T hat keine virtuellen Basisklassen
    Original:
    T has no virtual base classes
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Der Zuweisungsoperator fr jede direkte Basis von T gewhlt ist trivial
    Original:
    The copy assignment operator selected for every direct base of T is trivial
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Der Zuweisungsoperator fr jeden nicht-statische Klasse-Typ (oder ein Array von Klasse-Typ) memeber der T gewhlt ist trivial
    Original:
    The copy assignment operator selected for every non-static class type (or array of class type) memeber of T is trivial
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
Eine triviale Zuweisungsoperator macht eine Kopie des Objekts Darstellung wie von std::memmove. Alle Datentypen kompatibel mit der Programmiersprache C (POD-Typen) sind trivial copy-zuweisbare .
Original:
A trivial copy assignment operator makes a copy of the object representation as if by std::memmove. All data types compatible with the C language (POD types) are trivially copy-assignable.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Zuweisungsoperator implizit definiert

Wenn die implizit deklariert Zuweisungsoperator nicht gelscht oder trivial, ist es definiert (das heit, eine Funktion Krper erzeugt und kompiliert) durch den Compiler. Fr union Typen, kopiert das implizit definierten Zuweisungsoperator die Objekt-Reprsentation (wie std::memmove). Fr Nicht-union-Klasse-Typen (class und struct), fhrt der Bediener Mitglied-weise Kopie Zuordnung des Objekts Basen und nicht-statische Mitglieder, in deren Initialisierung Reihenfolge mit, Verwendung des eingebauten Zuordnung fr die Skalare und Zuweisungsoperator fr Klasse Typen .
Original:
If the implicitly-declared copy assignment operator is not deleted or trivial, it is defined (that is, a function body is generated and compiled) by the compiler. For union types, the implicitly-defined copy assignment copies the object representation (as by std::memmove). For non-union class types (class and struct), the operator performs member-wise copy assignment of the object's bases and non-static members, in their initialization order, using, using built-in assignment for the scalars and copy assignment operator for class types.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Die Erzeugung der implizit definierten Zuweisungsoperator ist deprecated(seit C++11) wenn T verfgt ber eine benutzerfreundliche erklrt destructor oder benutzerdefinierte erklrt Copykonstruktor .
Original:
The generation of the implicitly-defined copy assignment operator is deprecated(seit C++11) if T has a user-declared destructor or user-declared copy constructor.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Notes

Wenn beide kopieren und verschieben Zuweisungsoperatoren vorgesehen sind, whlt berladungsauflsung den Umzug Zuordnung, wenn das Argument ein rvalue (entweder prvalue wie eine namenlose temporre oder xWert, wie das Ergebnis der std::move ), und whlt den Zuweisungsoperator, wenn das Argument lvalue (benannte Objekt oder eine Funktion / Betreiber wieder lvalue Referenz). Wenn nur die Kopie Zuordnung haben, whlen Sie alle Argumente Kategorien es (wie lange es dauert ihr Argument als Wert oder als Verweis auf const, da rvalues const Referenzen binden knnen), was Zuweisungsoperator das Fallback fr unterwegs Zuordnung beim Bewegen nicht verfgbar ist .
Original:
If both copy and move assignment operators are provided, overload resolution selects the move assignment if the argument is an rvalue (either prvalue such as a nameless temporary or xvalue such as the result of std::move), and selects the copy assignment if the argument is lvalue (named object or a function/operator returning lvalue reference). If only the copy assignment is provided, all argument categories select it (as long as it takes its argument by value or as reference to const, since rvalues can bind to const references), which makes copy assignment the fallback for move assignment, when move is unavailable.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Kopieren und tauschen

Kopieren Zuweisungsoperator kann im Hinblick auf die Copy-Konstruktor, Destruktor und der Swap () Funktion ausgedrckt werden, wenn man zur Verfgung:
Original:
Copy assignment operator can be expressed in terms of copy constructor, destructor, and the swap() member function, if one is provided:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

T& T::operator=(T arg) { // copy/move constructor is called to construct arg swap(arg); // resources exchanged between *this and arg return *this; } // destructor is called to release the resources formerly held by *this

Fr Nicht-Werfen swap (), bietet diese Form starke Ausnahme Garantie. Fr rvalue Argumente, diese Form ruft automatisch die Bewegung Konstruktor und wird manchmal auch als "bergeordnete Zuweisungsoperator" (wie in, sowohl kopieren und verschieben) bezeichnet .
Original:
For non-throwing swap(), this form provides starke Ausnahme Garantie. For rvalue arguments, this form automatically invokes the move constructor, and is sometimes referred to as "unifying assignment operator" (as in, both copy and move).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Beispiel

#include <iostream>
#include <memory>
struct A {
    int n;
    std::string s1;
    // user-defined copy assignment, copy-and-swap form
    A& operator=(A other) {
        std::cout << "copy assignment of A\n";
        std::swap(n, other.n);
        std::swap(s1, other.s1);
        return *this;
    }
};

struct B : A {
    std::string s2;
    // implicitly-defined copy assignment
};

struct C {
     std::unique_ptr<int[]> data;
     std::size_t size;
     // non-copy-and-swap assignment
     C& operator=(const C& other) {
         // check for self-assignment
         if(&other == this)
             return *this;
         // reuse storage when possible
         if(size != other.size)
             data.reset(new int[other.size]);
         std::copy(&other.data[0],
                   &other.data[0] + std::min(size, other.size),
                   &data[0]);
         return *this;
     }
     // note: copy-and-swap would always cause a reallocation
};

int main()
{
    A a1, a2;
    std::cout << "a1 = a2 calls ";
    a1 = a2; // user-defined copy assignment

    B b1, b2;
    b2.s1 = "foo";
    b2.s2 = "bar";
    std::cout << "b1 = b2 calls ";
    b1 = b2; // implicitly-defined copy assignment
    std::cout << "b1.s1 = " << b1.s1 << " b1.s2 = " << b1.s2 <<  '\n';
}

Output:

a1 = a2 calls copy assignment of A
b1 = b2 calls copy assignment of A
b1.s1 = foo b1.s2 = bar

Web Proxy Viewer  |  New URL  |  Original Page