Call Assignment Operator Of Base Class For Anonymous Types

Chapter 9: Classes And Memory Allocation

In contrast to the set of functions that handle memory allocation in C (i.e., etc.), memory allocation in C++ is handled by the operators and . Important differences between and are:
  • The function doesn't `know' what the allocated memory will be used for. E.g., when memory for s is allocated, the programmer must supply the correct expression using a multiplication by . In contrast, requires a type to be specified; the expression is implicitly handled by the compiler. Using is therefore type safe.
  • Memory allocated by is initialized by , initializing the allocated characters to a configurable initial value. This is not very useful when objects are available. As operator knows about the type of the allocated entity it may (and will) call the constructor of an allocated class type object. This constructor may be also supplied with arguments.
  • All C-allocation functions must be inspected for -returns. This is not required anymore when is used. In fact, 's behavior when confronted with failing memory allocation is configurable through the use of a new_handler (cf. section 9.2.2).
A comparable relationship exists between and : makes sure that when an object is deallocated, its destructor is automatically called.

The automatic calling of constructors and destructors when objects are created and destroyed has consequences which we shall discuss in this chapter. Many problems encountered during C program development are caused by incorrect memory allocation or memory leaks: memory is not allocated, not freed, not initialized, boundaries are overwritten, etc.. C++ does not `magically' solve these problems, but it does provide us with tools to prevent these kinds of problems.

As a consequence of and friends becoming deprecated the very frequently used functions, like , that are all based, should be avoided in C++ programs. Instead, the facilities of the class and operators and should be used.

Memory allocation procedures influence the way classes dynamically allocating their own memory should be designed. Therefore, in this chapter these topics are discussed in addition to discussions about operators and . We'll first cover the peculiarities of operators and , followed by a discussion about:

  • the destructor: the member function that's called when an object ceases to exist;
  • the assignment operator, allowing us to assign an object to another object of its own class;
  • the pointer, allowing explicit references to the object for which a member function was called;
  • the copy constructor: the constructor creating a copy of an object;
  • the move constructor: a constructor creating an object from an anonymous temporary object.

9.1: Operators `new' and `delete'

C++ defines two operators to allocate memory and to return it to the `common pool'. These operators are, respectively, and .

Here is a simple example illustrating their use. An pointer variable points to memory allocated by operator . This memory is later released by operator .

int *ip = new int; delete ip;

Here are some characteristics of operators and :

  • and are operators and therefore do not require parentheses, as required for functions like and ;
  • returns a pointer to the kind of memory that's asked for by its operand (e.g., it returns a pointer to an );
  • uses a type as its operand, which has the important benefit that the correct amount of memory, given the type of the object to be allocated, is made available;
  • as a consequence, is a type safe operator as it always returns a pointer to the type that was mentioned as its operand. In addition, the type of the receiving pointer must match the type specified with operator ;
  • may fail, but this is normally of no concern to the programmer. In particular, the program does not have to test the success of the memory allocation, as is required for and friends. Section 9.2.2 delves into this aspect of ;
  • returns ;
  • for each call to a matching should eventually be executed, lest a memory leak occur;
  • can safely operate on a 0-pointer (doing nothing);
  • otherwise must only be used to return memory allocated by . It should not be used to return memory allocated by and friends.
  • in C++ and friends are deprecated and should be avoided.

Operator can be used to allocate primitive types but also to allocate objects. When a primitive type or a type without a constructor is allocated the allocated memory is not guaranteed to be initialized to 0, but an initialization expression may be provided:

int *v1 = new int; // not guaranteed to be initialized to 0 int *v1 = new int(); // initialized to 0 int *v2 = new int(3); // initialized to 3 int *v3 = new int(3 * *v2); // initialized to 9 When a class-type object is allocated, the arguments of its constructor (if any) are specified immediately following the type specification in the expression and the object is initialized by to the thus specified constructor. For example, to allocate objects the following statements could be used: string *s1 = new string; // uses the default constructor string *s2 = new string(); // same string *s3 = new string(4, ' '); // initializes to 4 blanks.

In addition to using to allocate memory for a single entity or an array of entities (see the next section) there also exists a variant allocating raw memory: . Raw memory is returned as a . Here allocates a block of memory for unspecified purpose. Although raw memory may consist of multiple characters it should not be interpreted as an array of characters. Since raw memory returned by is returned as a its return value can be assigned to a variable. More often it is assigned to a variable, using a cast. Here is an example:

char *chPtr = static_cast<char *>(operator new(numberOfBytes)); The use of raw memory is frequently encountered in combination with the placement new operator, discussed in section 9.1.5.

9.1.1: Allocating arrays

Operator is used to allocate arrays. The generic notation is used in the C++ Annotations. Actually, the number of elements to be allocated must be specified between the square brackets and it must, in turn, be prefixed by the type of the entities that must be allocated. Example: int *intarr = new int[20]; // allocates 20 ints string *stringarr = new string[10]; // allocates 10 strings. Operator is a different operator than operator . A consequence of this difference is discussed in the next section (9.1.2).

Arrays allocated by operator are called dynamic arrays. They are constructed during the execution of a program, and their lifetime may exceed the lifetime of the function in which they were created. Dynamically allocated arrays may last for as long as the program runs.

When is used to allocate an array of primitive values or an array of objects, must be specified with a type and an (unsigned) expression between its square brackets. The type and expression together are used by the compiler to determine the required size of the block of memory to make available. When is used the array's elements are stored consecutively in memory. An array index expression may thereafter be used to access the array's individual elements: represents the first value, immediately followed by , and so on until the last element ().

With non-class types (primitive types, POD types without constructors) the block of memory returned by operator is not guaranteed to be initialized to 0. Alternatively, adding to the expression will initialize the block of memory to zeroes. E.g.,

struct POD { int iVal; double dVal; }; new POD[5](); // returns a pointer to 5 0-initialized PODs new double[9](); // returns a pointer to 9 0-initialized doubles If there are members of the that are explicitly initialized in the struct's interface (e.g., ), or if the struct uses composition, and the composed data member's type defines a default constructor, then initializations in the struct's interface and initializations performed by the composed data member's constructor takes precedence over the 0-initialization. Here is an example: struct Data { int value = 100; }; struct POD { int iVal = 12; double dVal; Data data; }; POD *pp = new POD[5](); Here, points to five objects, each having their data members initialized to 12, their data members initialized to 0, and their members initialized to 100.

When operator is used to allocate arrays of objects of class types defining default constructors these constructors are automatically used. Consequently results in a block of 20 initialized objects. A non-default constructor cannot be called, but often it is possible to work around that (as discussed in section 13.8).

The expression between brackets of operator represents the number of elements of the array to allocate. The C++ standard allows allocation of 0-sized arrays. The statement is correct C++. However, it is also pointless and confusing and should be avoided. It is pointless as it doesn't refer to any element at all, it is confusing as the returned pointer has a useless non-0 value. A pointer intending to point to an array of values should be initialized (like any pointer that isn't yet pointing to memory) to 0, allowing for expressions like

Without using operator , arrays of variable sizes can also be constructed as local arrays. Such arrays are not dynamic arrays and their lifetimes are restricted to the lifetime of the block in which they were defined.

Once allocated, all arrays have fixed sizes. There is no simple way to enlarge or shrink arrays. C++ has no operator `'. Section 9.1.3 illustrates how to enlarge arrays.

9.1.2: Deleting arrays

Dynamically allocated arrays are deleted using operator . It expects a pointer to a block of memory, previously allocated by operator .

When operator 's operand is a pointer to an array of objects two actions are performed:

  • First, the class's destructor is called for each of the objects in the array. The destructor, as explained later in this chapter, performs all kinds of cleanup operations that are required by the time the object ceases to exist.
  • Second, the memory pointed at by the pointer is returned to the common pool.
Here is an example showing how to allocate and delete an array of 10 string objects: std::string *sp = new std::string[10]; delete[] sp; No special action is performed if a dynamically allocated array of primitive typed values is deleted. Following the statement simply returns the memory pointed at by . Realize that, as a pointer is a primitive type, deleting a dynamically allocated array of pointers to objects does not result in the proper destruction of the objects the array's elements point at. So, the following example results in a memory leak: string **sp = new string *[5]; for (size_t idx = 0; idx != 5; ++idx) sp[idx] = new string; delete[] sp; // MEMORY LEAK ! In this example the only action performed by is to return an area the size of five pointers to strings to the common pool.

Here's how the destruction in such cases should be performed:

  • Call for each of the array's elements;
  • Delete the array itself
Example: for (size_t idx = 0; idx != 5; ++idx) delete sp[idx]; delete[] sp; One of the consequences is of course that by the time the memory is going to be returned not only the pointer must be available but also the number of elements it contains. This can easily be accomplished by storing pointer and number of elements in a simple class and then using an object of that class.

Operator is a different operator than operator . The rule of thumb is: if was used, also use .

9.1.3: Enlarging arrays

Once allocated, all arrays have fixed sizes. There is no simple way to enlarge or shrink arrays. C++ has no operator. The basic steps to take when enlarging an array are the following:
  • Allocate a new block of memory of larger size;
  • Copy the old array contents to the new array;
  • Delete the old array;
  • Let the pointer to the array point to the newly allocated array.
Static and local arrays cannot be resized. Resizing is only possible for dynamically allocated arrays. Example: #include <string> using namespace std; string *enlarge(string *old, size_t oldsize, size_t newsize) { string *tmp = new string[newsize]; // allocate larger array for (size_t idx = 0; idx != oldsize; ++idx) tmp[idx] = old[idx]; // copy old to tmp delete[] old; // delete the old array return tmp; // return new array } int main() { string *arr = new string[4]; // initially: array of 4 strings arr = enlarge(arr, 4, 6); // enlarge arr to 6 elements. }

The procedure to enlarge shown in the example also has several drawbacks.

  • The new array requires constructors to be called;
  • Having initialized the strings in the new array, of them are immediately reassigned to the corresponding values in the original array;
  • All the objects in the old arrays are destroyed.
Depending on the context various solutions exist to improve the efficiency of this rather inefficient procedure. An array of pointers could be used (requiring only the pointers to be copied, no destruction, no superfluous initialization) or raw memory in combination with the placement new operator could be used (an array of objects remains available, no destruction, no superfluous construction).

9.1.4: Managing `raw' memory

As we've seen operator allocates the memory for an object and subsequently initializes that object by calling one of its constructors. Likewise, operator calls an object's destructor and subsequently returns the memory allocated by operator to the common pool.

In the next section we'll encounter another use of , allowing us to initialize objects in so-called raw memory: memory merely consisting of bytes that have been made available by either static or dynamic allocation.

Raw memory is made available by and also by . The returned memory should not be interpreted as an array of any kind but just a series of memory locations that were dynamically made available. No initialization whatsoever is performed by these variants of .

Both variants return so (static) casts are required to use the return values as memory of some type.

Here are two examples:

// room for 5 ints: int *ip = static_cast<int *>(operator new(5 * sizeof(int))); // same as the previous example: int *ip2 = static_cast<int *>(operator new[](5 * sizeof(int))); // room for 5 strings: string *sp = static_cast<string *>(operator new(5 * sizeof(string))); As has no concept of data types the size of the intended data type must be specified when allocating raw memory for a certain number of objects of an intended type. The use of therefore somewhat resembles the use of .

The counterpart of is . (or, equivalently, ), expects a (so a pointer to any type can be passed to it). The pointer is interpreted as a pointer to raw memory which is returned to the common pool without any further action. In particular, no destructors are called by . The use of therefore resembles the use of . To return the memory pointed at by the abovementioned variables and should be used:

// delete raw memory allocated by operator new operator delete(ip); operator delete[](ip2); operator delete(sp);

9.1.5: The `placement new' operator

A remarkable form of operator is called the placement new operator. Before using placement the header file must be included.

Placement is passed an existing block of memory into which initializes an object or value. The block of memory should be large enough to contain the object, but apart from that there are no further requirements. It is easy to determine how much memory is used by en entity (object or variable) of type : the operator returns the number of bytes used by an entity.

Entities may of course dynamically allocate memory for their own use. Dynamically allocated memory, however, is not part of the entity's memory `footprint' but it is always made available externally to the entity itself. This is why returns the same value when applied to different objects that return different length and capacity values.

The placement operator uses the following syntax (using to indicate the used data type):

Type *new(void *memory) Type{ arguments }; Here, is a block of memory of at least bytes and is any constructor of the class .

The placement operator is useful in situations where classes set aside memory to be used later. This is used, e.g., by to change its capacity. Calling may enlarge that capacity without making memory beyond the string's length immediately available to the object's users. But the object itself may use its additional memory. E.g, when information is added to a object it can draw memory from its capacity rather than performing a reallocation for each single character that is added to its contents.

Let's apply that philosophy to a class storing objects. The class defines a accessing the memory holding its string objects as well as reserved memory. Assuming that a default constructor initializes to 1, doubling whenever an additional must be stored, the class must support the following essential operations:

  • doubling its capacity when all its spare memory (e.g., made available by ) has been consumed;
  • adding another object
  • properly deleting the installed strings and memory when a object ceases to exist.
The private member is called when the current capacity must be enlarged to . It operates as follows: First new, raw, memory is allocated (line 1). This memory is in no way initialized with strings. Then the available strings in the old memory are copied into the newly allocated raw memory using placement new (line 2). Next, the old memory is deleted (line 3). void Strings::reserve() { using std::string; string *newMemory = static_cast<string *>( // 1 operator new(d_capacity * sizeof(string))); for (size_t idx = 0; idx != d_size; ++idx) // 2 new (newMemory + idx) string(d_memory[idx]); destroy(); // 3 d_memory = newMemory; }

The member adds another object to a object. A (public) member (enlarging if necessary and if enlarged calling ) ensures that the object's capacity is sufficient. Then placement is used to install the latest string into the raw memory's appropriate location:

void Strings::append(std::string const &next) { reserve(d_size + 1); new (d_memory + d_size) std::string{ next }; ++d_size; }

At the end of the object's lifetime, and during enlarging operations all currently used dynamically allocated memory must be returned. This is made the responsibility of the member , which is called by the class's destructor and by . More about the destructor itself in the next section, but the implementation of the support member is discussed below.

With placement an interesting situation is encountered. Objects, possibly themselves allocating memory, are installed in memory that may or may not have been allocated dynamically, but that is usually not completely filled with such objects. So a simple can't be used. On the other hand, a for each of the objects that are available can't be used either, since those operations would also try to delete the memory of the objects themselves, which wasn't dynamically allocated.

This peculiar situation is solved in a peculiar way, only encountered in cases where placement is used: memory allocated by objects initialized using placement is returned by explicitly calling the object's destructor. The destructor is declared as a member having as its name the class name preceded by a tilde, not using any arguments. So, 's destructor is named . An object's destructor only returns memory allocated by the object itself and, despite of its name, does not destroy its object. Any memory allocated by the strings stored in our class is therefore properly destroyed by explicitly calling their destructors. Following this is back to its initial status: it again points to raw memory. This raw memory is then returned to the common pool by :

void Strings::destroy() { for (std::string *sp = d_memory + d_size; sp-- != d_memory; ) sp->~string(); operator delete(d_memory); }

So far, so good. All is well as long as we're using only one object. What about allocating an array of objects? Initialization is performed as usual. But as with , cannot be called when the buffer was allocated statically. Instead, when multiple objects were initialized using placement in combination with a statically allocated buffer all the objects' destructors must be called explicitly, as in the following example:

using std::string; char buffer[3 * sizeof(string)]; string *sp = new(buffer) string [3]; for (size_t idx = 0; idx < 3; ++idx) sp[idx].~string();

9.2: The destructor

Comparable to the constructor, classes may define a destructor. This function is the constructor's counterpart in the sense that it is invoked when an object ceases to exist. A destructor is usually called automatically, but that's not always true. The destructors of dynamically allocated objects are not automatically activated, but in addition to that: when a program is interrupted by an call, only the destructors of already initialized global objects are called. In that situation destructors of objects defined locally by functions are also not called. This is one (good) reason for avoiding in C++ programs.

Destructors obey the following syntactical requirements:

  • a destructor's name is equal to its class name prefixed by a tilde;
  • a destructor has no arguments;
  • a destructor has no return value.
Destructors are declared in their class interfaces. Example: class Strings { public: Strings(); ~Strings(); // the destructor }; By convention the constructors are declared first. The destructor is declared next, to be followed by other member functions.

A destructor's main task is to ensure that memory allocated by an object is properly returned when the object ceases to exist. Consider the following interface of the class :

class Strings { std::string *d_string; size_t d_size; public: Strings(); Strings(char const *const *cStrings, size_t n); ~Strings(); std::string const &at(size_t idx) const; size_t size() const; };

The constructor's task is to initialize the data fields of the object. E.g, its constructors are defined as follows:

Strings::Strings() : d_string(0), d_size(0) {} Strings::Strings(char const *const *cStrings, size_t size) : d_string(new string[size]), d_size(size) { for (size_t idx = 0; idx != size; ++idx) d_string[idx] = cStrings[idx]; } As objects of the class allocate memory a destructor is clearly required. Destructors may or may not be called automatically, but note that destructors are only called (or, in the case of dynamically allocated objects: should only be called) for fully constructed objects.

C++ considers objects `fully constructed' once at least one of its constructors could normally complete. It used to be the constructor, but as C++ supports constructor delegation, multiple constructors can be activated for a single object; hence `at least one constructor'. The remaining rules apply to fully constructed objects;

  • Destructors of local non-static objects are called automatically when the execution flow leaves the block in which they were defined; the destructors of objects defined somewhere in the outer block of a function are called just before the function terminates.
  • Destructors of static or global objects are called when the program itself terminates.
  • The destructor of a dynamically allocated object is called by using the object's address as its operand;
  • The destructors of a dynamically allocated array of objects are called by using the address of the array's first element as its operand;
  • The destructor of an object initialized by placement is activated by explicitly calling the object's destructor.
The destructor's task is to ensure that all memory that is dynamically allocated and controlled only by the object itself is returned. The task of the 's destructor would therefore be to delete the memory to which points. Its implementation is: Strings::~Strings() { delete[] d_string; }

The next example shows at work. In a is created, and its data are displayed. It returns a dynamically allocated object to . A receives the address of the allocated object and deletes the object again. Another object is then created in a block of memory made available locally in , and an explicit call to is required to return the memory allocated by that object. In the example only once a object is automatically destroyed: the local object defined by . The other two objects require explicit actions to prevent memory leaks.

#include "strings.h" #include <iostream> using namespace std;; void display(Strings const &store) { for (size_t idx = 0; idx != store.size(); ++idx) cout << << '\n'; } Strings *process(char *argv[], size_t argc) { Strings store{ argv, argc }; display(store); return new Strings{ argv, argc }; } int main(int argc, char *argv[]) { Strings *sp = process(argv, argc); delete sp; char buffer[sizeof(Strings)]; sp = new (buffer) Strings{ argv, static_cast<size_t>(argc) }; sp->~Strings(); }

9.2.1: Object pointers revisited

Operators and are used when an object or variable is allocated. One of the advantages of the operators and over functions like and is that and call the corresponding object constructors and destructors.

The allocation of an object by operator is a two-step process. First the memory for the object itself is allocated. Then its constructor is called, initializing the object. Analogously to the construction of an object, the destruction is also a two-step process: first, the destructor of the class is called deleting the memory controlled by the object. Then the memory used by the object itself is freed.

Dynamically allocated arrays of objects can also be handled by and . When allocating an array of objects using operator the default constructor is called for each object in the array. In cases like this operator must be used to ensure that the destructor is called for each of the objects in array.

However, the addresses returned by and are of identical types, in both cases a . Consequently it cannot be determined by the type of the pointer whether a pointer to dynamically allocated memory points to a single entity or to an array of entities.

What happens if rather than is used? Consider the following situation, in which the destructor is modified so that it tells us that it is called. In a function an array of two objects is allocated using , to be deleted by . Next, the same actions are repeated, albeit that the operator is called without :

#include <iostream> #include "strings.h" using namespace std; Strings::~Strings() { cout << "Strings destructor called" << '\n'; } int main() { Strings *a = new Strings[2]; cout << "Destruction with []'s" << '\n'; delete[] a; a = new Strings[2]; cout << "Destruction without []'s" << '\n'; delete a; } /* Generated output: Destruction with []'s Strings destructor called Strings destructor called Destruction without []'s Strings destructor called */ From the generated output, we see that the destructors of the individual objects are called when is used, while only the first object's destructor is called if the is omitted.

Conversely, if is called in a situation where should have been called the results are unpredictable, and the program will most likely crash. This problematic behavior is caused by the way the run-time system stores information about the size of the allocated array (usually right before the array's first element). If a single object is allocated the array-specific information is not available, but it is nevertheless assumed present by . Thus this latter operator encounters bogus values in the memory locations just before the array's first element. It then dutifully interprets the value it encounters there as size information, usually causing the program to fail.

If no destructor is defined, a trivial destructor is defined by the compiler. The trivial destructor ensures that the destructors of composed objects (as well as the destructors of base classes if a class is a derived class, cf. chapter 13) are called. This has serious implications: objects allocating memory create memory leaks unless precautionary measures are taken (by defining an appropriate destructor). Consider the following program:

#include <iostream> #include "strings.h" using namespace std; Strings::~Strings() { cout << "Strings destructor called" << '\n'; } int main() { Strings **ptr = new Strings* [2]; ptr[0] = new Strings[2]; ptr[1] = new Strings[2]; delete[] ptr; } This program produces no output at all. Why is this? The variable is defined as a pointer to a pointer. The dynamically allocated array therefore consists of pointer variables and pointers are of a primitive type. No destructors exist for primitive typed variables. Consequently only the array itself is returned, and no destructor is called.

Of course, we don't want this, but require the objects pointed to by the elements of to be deleted too. In this case we have two options:

  • In a for-statement visit all the elements of the array, calling for each of the array's elements. This procedure was demonstrated in the previous section.
  • A wrapper class is designed around a pointer (to, e.g., an object of some class, like ). Rather than using a pointer to a pointer to objects a pointer to an array of wrapper-class objects is used. As a result calls the destructor of each of the wrapper class objects, in turn calling the destructor for their members. Example: #include <iostream> using namespace std; class Strings // partially implemented { public: ~Strings(); }; inline Strings::~Strings() { cout << "destructor called\n"; } class Wrapper { Strings *d_strings; public: Wrapper(); ~Wrapper(); }; inline Wrapper::Wrapper() : d_strings(new Strings{}) {} inline Wrapper::~Wrapper() { delete d_strings; } int main() { auto ptr = new Strings *[4]; // ... code assigning `new Strings' to ptr's elements delete[] ptr; // memory leak: ~Strings() not called cout << "===========\n"; delete[] new Wrapper[4]; // OK: 4 x destructor called } /* Generated output: =========== destructor called destructor called destructor called destructor called */

9.2.2: The function set_new_handler()

The C++ run-time system ensures that when memory allocation fails an error function is activated. By default this function throws a bad_alloc exception (see section 10.8), terminating the program. Therefore it is not necessary to check the return value of operator . Operator 's default behavior may be modified in various ways. One way to modify its behavior is to redefine the function that's called when memory allocation fails. Such a function must comply with the following requirements:
  • it has no parameters;
  • its return type is .

A redefined error function might, e.g., print a message and terminate the program. The user-written error function becomes part of the allocation system through the function .

Such an error function is illustrated below ( This implementation applies to the Gnu C/C++ requirements. Actually using the program given in the next example is not advised, as it probably enormously slows down your computer due to the resulting use of the operating system's swap area.):

#include <iostream> #include <string> using namespace std; void outOfMemory() { cout << "Memory exhausted. Program terminates." << '\n'; exit(1); } int main() { long allocated = 0; set_new_handler(outOfMemory); // install error function while (true) // eat up all memory { new int [100000](); allocated += 100000 * sizeof(int); cout << "Allocated " << allocated << " bytes\n"; } } Once the new error function has been installed it is automatically invoked when memory allocation fails, and the program is terminated. Memory allocation may fail in indirectly called code as well, e.g., when constructing or using streams or when strings are duplicated by low-level functions.

So far for the theory. On some systems the `out of memory' condition may actually never be reached, as the operating system may interfere before the run-time support system gets a chance to stop the program (see also this url).

The standard C functions allocating memory (like , , etc.) do not trigger the handler when memory allocation fails and should be avoided in C++ programs.

9.3: The assignment operator

In C++ struct and class type objects can be directly assigned new values in the same way as this is possible in C. The default action of such an assignment for non-class type data members is a straight byte-by-byte copy from one data member to another. For now we'll use the following simple class : class Person { char *d_name; char *d_address; char *d_phone; public: Person(); Person(char const *name, char const *addr, char const *phone); ~Person(); private: char *strdupnew(char const *src); // returns a copy of src. }; // strdupnew is easily implemented, here is its inline implementation: inline char *Person::strdupnew(char const *src) { return strcpy(new char [strlen(src) + 1], src); } 's data members are initialized to zeroes or to copies of the NTBSs passed to 's constructor, using some variant of . The allocated memory is eventually returned by 's destructor.

Now consider the consequences of using objects in the following example:

void tmpPerson(Person const &person) { Person tmp; tmp = person; } Here's what happens when is called:
  • it expects a reference to a as its parameter .
  • it defines a local object , whose data members are initialized to zeroes.
  • the object referenced by is copied to : number of bytes are copied from to .
Now a potentially dangerous situation has been created. The actual values in are pointers, pointing to allocated memory. After the assignment this memory is addressed by two objects: and.
  • The potentially dangerous situation develops into an acutely dangerous situation once the function terminates: is destroyed. The destructor of the class releases the memory pointed to by the fields , and : unfortunately, this memory is also pointed at by ....
This problematic assignment is illustrated in Figure 4.

Having executed , the object referenced by now contains pointers to deleted memory.

This is undoubtedly not a desired effect of using a function like . The deleted memory is likely to be reused by subsequent allocations. The pointer members of have effectively become wild pointers, as they don't point to allocated memory anymore. In general it can be concluded that

every class containing pointer data members is a potential candidate for trouble.

Fortunately, it is possible to prevent these troubles, as discussed next.

9.3.1: Overloading the assignment operator

Obviously, the right way to assign one object to another, is not to copy the contents of the object bytewise. A better way is to make an equivalent object. One having its own allocated memory containing copies of the original strings.

The way to assign a object to another is illustrated in Figure 5.

There are several ways to assign a object to another. One way would be to define a special member function to handle the assignment. The purpose of this member function would be to create a copy of an object having its own , and strings. Such a member function could be: void Person::assign(Person const &other) { // delete our own previously used memory delete[] d_name; delete[] d_address; delete[] d_phone; // copy the other Person's data d_name = strdupnew(other.d_name); d_address = strdupnew(other.d_address); d_phone = strdupnew(other.d_phone); } Using we could rewrite the offending function : void tmpPerson(Person const &person) { Person tmp; // tmp (having its own memory) holds a copy of person tmp.assign(person); // now it doesn't matter that tmp is destroyed.. } This solution is valid, although it only tackles a symptom. It requires the programmer to use a specific member function instead of the assignment operator. The original problem (assignment produces wild pointers) is still not solved. Since it is hard to `strictly adhere to a rule' a way to solve the original problem is of course preferred.

Fortunately a solution exists using operator overloading: the possibility C++ offers to redefine the actions of an operator in a given context. Operator overloading was briefly mentioned earlier, when the operators << and >> were redefined to be used with streams (like , and ), see section 3.1.4.

Overloading the assignment operator is probably the most common form of operator overloading in C++. A word of warning is appropriate, though. The fact that C++ allows operator overloading does not mean that this feature should indiscriminately be used. Here's what you should keep in mind:

  • operator overloading should be used in situations where an operator has a defined action, but this default action has undesired side effects in a given context. A clear example is the above assignment operator in the context of the class .
  • operator overloading can be used in situations where the operator is commonly applied and no surprise is introduced when it's redefined. An example where operator overloading is appropriately used is found in the class : assigning one string object to another provides the destination string with a copy of the contents of the source string. No surprises here.
  • in all other cases a member function should be defined instead of redefining an operator.
An operator should simply do what it is designed to do. The phrase that's often encountered in the context of operator overloading is do as the s do. The way operators behave when applied to s is what is expected, all other implementations probably cause surprises and confusion. Therefore, overloading the insertion (<<) and extraction (>>) operators in the context of streams is probably ill-chosen: the stream operations have nothing in common with bitwise shift operations. The member 'operator=()'

To add operator overloading to a class, the class interface is simply provided with a (usually public) member function naming the particular operator. That member function is thereupon implemented.

To overload the assignment operator , a member is added to the class interface. Note that the function name consists of two parts: the keyword , followed by the operator itself. When we augment a class interface with a member function , then that operator is redefined for the class, which prevents the default operator from being used. In the previous section the function was provided to solve the problems resulting from using the default assignment operator. Rather than using an ordinary member function C++ commonly uses a dedicated operator generalizing the operator's default behavior to the class in which it is defined.

The member mentioned before may be redefined as follows (the member presented below is a first, rather unsophisticated, version of the overloaded assignment operator. It will shortly be improved):

class Person { public: // extension of the class Person // earlier members are assumed. void operator=(Person const &other); }; Its implementation could be void Person::operator=(Person const &other) { delete[] d_name; // delete old data delete[] d_address; delete[] d_phone; d_name = strdupnew(other.d_name); // duplicate other's data d_address = strdupnew(other.d_address); d_phone = strdupnew(other.d_phone); } This member's actions are similar to those of the previously mentioned member , but this member is automatically called when the assignment operator is used. Actually there are two ways to call overloaded operators as shown in the next example: void tmpPerson(Person const &person) { Person tmp; tmp = person; tmp.operator=(person); // the same thing } Overloaded operators are seldom called explicitly, but explicit calls must be used (rather than using the plain operator syntax) when you explicitly want to call the overloaded operator from a pointer to an object (it is also possible to dereference the pointer first and then use the plain operator syntax, see the next example): void tmpPerson(Person const &person) { Person *tmp = new Person; tmp->operator=(person); *tmp = person; // yes, also possible... delete tmp; }

9.4: The `this' pointer

A member function of a given class is always called in combination with an object of its class. There is always an implicit `substrate' for the function to act on. C++ defines a keyword, , to reach this substrate.

The keyword is a pointer variable that always contains the address of the object for which the member function was called. The pointer is implicitly declared by each member function (whether , or ). The pointer is a constant pointer to an object of the member function's class. For example, the members of the class implicitly declare:

extern Person *const this; A member function like could be implemented in two ways: with or without using the pointer: char const *Person::name() const // implicitly using `this' { return d_name; } char const *Person::name() const // explicitly using `this' { return this->d_name; } The pointer is seldom explicitly used, but situations do exist where the pointer is actually required (cf. chapter 16).

9.4.1: Sequential assignments and this

C++'s syntax allows for sequential assignments, with the assignment operator associating from right to left. In statements like: a = b = c; the expression is evaluated first, and its result in turn is assigned to .

The implementation of the overloaded assignment operator we've encountered thus far does not permit such constructions, as it returns .

This imperfection can easily be remedied using the pointer. The overloaded assignment operator expects a reference to an object of its class. It can also return a reference to an object of its class. This reference can then be used as an argument in sequential assignments.

The overloaded assignment operator commonly returns a reference to the current object (i.e., ). The next version of the overloaded assignment operator for the class thus becomes:

Person &Person::operator=(Person const &other) { delete[] d_address; delete[] d_name; delete[] d_phone; d_address = strdupnew(other.d_address); d_name = strdupnew(other.d_name); d_phone = strdupnew(other.d_phone); // return current object as a reference return *this; } Overloaded operators may themselves be overloaded. Consider the class, having overloaded assignment operators , and several more overloaded versions. These additional overloaded versions are there to handle different situations which are, as usual, recognized by their argument types. These overloaded versions all follow the same mold: when necessary dynamically allocated memory controlled by the object is deleted; new values are assigned using the overloaded operator's parameter values and is returned.

9.5: The copy constructor: initialization vs. assignment

Consider the class , introduced in section 9.2, once again. As it contains several primitive type data members as well as a pointer to dynamically allocated memory it needs a constructor, a destructor, and an overloaded assignment operator. In fact the class offers two constructors: in addition to the default constructor it offers a constructor expecting a and a .

Now consider the following code fragment. The statement references are discussed following the example:

int main(int argc, char **argv) { Strings s1(argv, argc); // (1) Strings s2; // (2) Strings s3(s1); // (3) s2 = s1; // (4) }
  • At 1 we see an initialization. The object is initialized using 's parameters: 's second constructor is used.
  • At 2 's default constructor is used, initializing an empty object.
  • At 3 yet another object is created, using a constructor accepting an existing object. This form of initializations has not yet been discussed. It is called a copy construction and the constructor performing the initialization is called the copy constructor. Copy constructions are also encountered in the following form: Strings s3 = s1; This is a construction and therefore an initialization. It is not an assignment as an assignment needs a left-hand operand that has already been defined. C++ allows the assignment syntax to be used for constructors having only one parameter. It is somewhat deprecated, though.
  • At 4 we see a plain assignment.
In the above example three objects were defined, each using a different constructor. The actually used constructor was deduced from the constructor's argument list.

The copy constructor encountered here is new. It does not result in a compilation error even though it hasn't been declared in the class interface. This takes us to the following rule:

A copy constructor is (almost) always available, even if it isn't declared in the class's interface.
The reason for the `(almost)' is given in section 9.7.1.

The copy constructor made available by the compiler is also called the trivial copy constructor. Its use can easily be suppressed (using the idiom). The trivial copy constructor performs a byte-wise copy operation of the existing object's primitive data to the newly created object, calls copy constructors to intialize the object's class data members from their counterparts in the existing object and, when inheritance is used, calls the copy constructors of the base class(es) to initialize the new object's base classes.

Consequently, in the above example the trivial copy constructor is used. As it performs a byte-by-byte copy operation of the object's primitive type data members that is exactly what happens at statement 3. By the time ceases to exist its destructor deletes its array of strings. Unfortunately is of a primitive data type and so it also deletes 's data. Once again we encounter wild pointers as a result of an object going out of scope.

The remedy is easy: instead of using the trivial copy constructor a copy constructor must explicitly be added to the class's interface and its definition must prevent the wild pointers, comparably to the way this was realized in the overloaded assignment operator. An object's dynamically allocated memory is duplicated, so that it contains its own allocated data. The copy constructor is simpler than the overloaded assignment operator in that it doesn't have to delete previously allocated memory. Since the object is going to be created no memory has already been allocated.

's copy constructor can be implemented as follows:

Strings::Strings(Strings const &other) : d_string(new string[other.d_size]), d_size(other.d_size) { for (size_t idx = 0; idx != d_size; ++idx) d_string[idx] = other.d_string[idx]; }

The copy constructor is always called when an object is initialized using another object of its class. Apart from the plain copy construction that we encountered thus far, here are other situations where the copy constructor is used:

  • it is used when a function defines a class type value parameter rather than a pointer or a reference. The function's argument initializes the function's parameter using the copy constructor. Example: void process(Strings store) // no pointer, no reference { = "modified"; // doesn't modify `outer' } int main(int argc, char **argv) { Strings outer(argv, argc); process(outer); }
  • it is used when a function defines a class type value return type. Example: Strings copy(Strings const &store) { return store; }
Here is used to initialize 's return value. The returned object is a temporary, anonymous object that may be immediately used by code calling but no assumptions can be made about its lifetime thereafter.

9.6: Revising the assignment operator

The overloaded assignment operator has characteristics also encountered with the copy constructor and the destructor:
  • The copying of (private) data occurs (1) in the copy constructor and (2) in the overloaded assignment function.
  • Allocated memory is deleted (1) in the overloaded assignment function and (2) in the destructor.
The copy constructor and the destructor clearly are required. If the overloaded assignment operator also needs to return allocated memory and to assign new values to its data members couldn't the destructor and copy constructor be used for that?

As we've seen in our discussion of the destructor (section 9.2) the destructor can explicitly be called, but that doesn't hold true for the (copy) constructor. But let's briefly summarize what an overloaded assignment operator is supposed to do:

  • It should delete the dynamically allocated memory controlled by the current object;
  • It should reassign the current object's data members using a provided existing object of its class.
The second part surely looks a lot like copy construction. Copy construction becomes even more attractive after realizing that the copy constructor also initializes any reference data members the class might have. Realizing the copy construction part is easy: just define a local object and initialize it using the assignment operator's const reference parameter, like this: Strings &operator=(Strings const &other) { Strings tmp(other); // more to follow return *this; } You may think the optimization is attractive, but let's postpone that for a little while (at least until section 9.7).

Now that we've done the copying part, what about the deleting part? And isn't there another slight problem as well? After all we copied all right, but not into our intended (current, ) object.

At this point it's time to introduce swapping. Swapping two variables means that the two variables exchange their values. We'll discuss swapping in detail in the next section, but let's for now assume that we've added a member to our class . This allows us to complete 's implementation:

Strings &operator=(Strings const &other) { Strings tmp(other); swap(tmp); return *this; } This implementation of is generic: it can be applied to every class whose objects are swappable. How does it work?
  • The information in the object is used to initialize a local object. This takes care of the copying part of the assignment operator;
  • Calling ensures that the current object receives its new values (with receiving the current object's original values);
  • When terminates its local object ceases to exist and its destructor is called. As it by now contains the data previously owned by the current object, the current object's original data are now destroyed, effectively completing the destruction part of the assignment operation.

9.6.1: Swapping

Many classes (e.g., ) offer members allowing us to swap two of their objects. The Standard Template Library (STL, cf. chapter 18) offers various functions related to swapping. There is even a generic algorithm (cf. section 19.1.61), which is commonly implemented using the assignment operator. When implementing a member for our class it could be used, provided that all of 's data members can be swapped. As this is true (why this is true is discussed shortly) we can augment with a member: void Strings::swap(Strings &other) { swap(d_string, other.d_string); swap(d_size, other.d_size); } Having added this member to the copy-and-swap implementation of can now be used.

When two variables (e.g., and ) are swapped, each one holds the other one's value after the swap. So, if and then after and .

Variables of primitive data types (pointers and the built-in types) can be swapped, class-type objects can be swapped if their classes offer a member.

So should we provide our classes with a swap member, and if so, how should it be implemented?

The above example () shows the standard way to implement a member: each of its data members are swapped in turn. But there are situations where a class cannot implement a swap member this way, even if the class only defines data members of primitive data types. Consider the situation depicted in figure 6.

In this figure there are four objects, each object has a pointer pointing to the next object. The basic organization of such a class looks like this:

class List { List *d_next; ... }; Initially four objects have their pointer set to the next object: 1 to 2, 2 to 3, 3 to 4. This is shown in the upper half of the figure. At the bottom half it is shown what happens if objects 2 and 3 are swapped: 3's point is now at object 2, which still points to 4; 2's pointer points to 3's address, but 2's is now at object 3, which is therefore pointing to itself. Bad news!

Another situation where swapping of objects goes wrong happens with classes having data members pointing or referring to data members of the same object. Such a situation is shown in figure 7.

Here, objects have two data members, as in the following class setup:

class SelfRef { size_t *d_ownPtr; // initialized to &d_data size_t d_data; }; The top-half of figure 7 shows two objects; their upper data members pointing to their lower data members. But if these objects are swapped then the situation shown in the figure's bottom half is encountered. Here the values at addresses a and c are swapped, and so, rather than pointing to their bottom data members they suddenly point to other object's data members. Again: bad news.

The common cause of these failing swapping operations is easily recognized: simple swapping operations must be avoided when data members point or refer to data that is involved in the swapping. If, in figure 7 the a and c data members would point to information outside of the two objects (e.g., if they would point to dynamically allocated memory) then the simple swapping would succeed.

However, the difficulty encountered with swapping objects does not imply that two objects cannot be swapped; it only means that we must be careful when designing members. Here is an implementation of :

void SelfRef::swap(SelfRef &other) { swap(d_data, other.d_data); } In this implementation swapping leaves the self-referential data member as-is, and merely swaps the remaining data. A similar member could be designed for the linked list shown in figure 6. Fast swapping

As we've seen with placement objects can be constructed in blocks of memory of bytes large. And so, two objects of the same class each occupy bytes.

If objects of our class can be swapped, and if our class's data members do not refer to data actually involved in the swapping operation then a very fast swapping method that is based on the fact that we know how large our objects are can be implemented.

In this fast-swap method we merely swap the contents of the bytes. This procedure may be applied to classes whose objects may be swapped using a member-by-member swapping operation and can (in practice, although this probably overstretches the allowed operations as described by the C++ ANSI/ISO standard) also be used in classes having reference data members. It simply defines a buffer of bytes and performs a circular operation. Here is its implementation for a hypothetical class . It results in very fast swapping:

#include <cstring> void Class::swap(Class &other) { char buffer[sizeof(Class)]; memcpy(buffer, &other, sizeof(Class)); memcpy(&other, this, sizeof(Class)); memcpy(this, buffer, sizeof(Class)); }

Here is a simple example of a class defining a reference data member and offering a member implemented like the one above. The reference data members are initialized to external streams. After running the program contains two hello to 1 lines, contains two hello to 2 lines (for brevity all members of are defined inline):

#include <fstream> #include <cstring> class Reference { std::ostream &d_out; public: Reference(std::ostream &out) : d_out(out) {} void swap(Reference &other) { char buffer[sizeof(Reference)]; memcpy(buffer, this, sizeof(Reference)); memcpy(this, &other, sizeof(Reference)); memcpy(&other, buffer, sizeof(Reference)); } std::ostream &out() { return d_out; } }; int main() { std::ofstream one{ "one" }; std::ofstream two{ "two" }; Reference ref1{ one }; // ref1/ref2 hold references to Reference ref2{ two }; // the streams ref1.out() << "hello to 1\n"; // generate some output ref2.out() << "hello to 2\n"; ref1.swap(ref2); ref2.out() << "hello to 1\n"; // more output ref1.out() << "hello to 2\n"; }

Fast swapping should only be used for self-defined classes for which it can be proven that fast-swapping does not corrupt its objects, when swapped.

9.7: Moving data

Traditionally, C++ offered two ways to assign the information pointed to by a data member of a temporary object to an lvalue object. Either a copy constructor or reference counting had to be used. In addition to these two methods C++ now also supports move semantics, allowing transfer of the data pointed to by a temporary object to its destination.

Moving information is based on the concept of anonymous (temporary) data. Temporary values are returned by functions like and , and in general by functions returning their results `by value' instead of returning references or pointers.

Anonymous values are always short-lived. When the returned values are primitive types (, etc.) nothing special happens, but if a class-type object is returned by value then its destructor can be called immediately following the function call that produced the value. In any case, the value itself becomes inaccessible immediately after the call. Of course, a temporary return value may be bound to a reference (lvalue or rvalue), but as far as the compiler is concerned the value now has a name, which by itself ends its status as a temporary value.

In this section we concentrate on anonymous temporary values and show how they can be used to improve the efficiency of object construction and assignment. These special construction and assignment methods are known as move construction and move assignment. Classes supporting move operations are called move-aware.

Classes allocating their own memory usually benefit from becoming move-aware. But a class does not have to use dynamic memory allocation before it can benefit from move operations. Most classes using composition (or inheritance where the base class uses composition) can benefit from move operations as well.

Movable parameters for class take the form . The parameter is an rvalue reference, and a rvalue reference only binds to an anonymous temporary value. The compiler is required to call functions offering movable parameters whenever possible. This happens when the class defines functions supporting parameters and an anonymous temporary value is passed to such functions. Once a temporary value has a name (which already happens inside functions defining or parameters as within such functions the names of these parameters are available) it is no longer an anonymous temporary value, and within such functions the compiler no longer calls functions expecting anonymous temporary values when the parameters are used as arguments.

The next example (using inline member implementations for brevity) illustrates what happens if a non-const object, a temporary object and a const object are passed to functions for which these kinds of parameters were defined. Each of these functions call a function for which these kinds of parameters were also defined. The first time is called it (as expected) calls . Then is called as its argument is an anonymous (temporary) object. However, inside the anonymous value has received a name, and so it isn't anonymous anymore. Consequently, is called once again. Finally is called, and (as expected) is now called.

#include <iostream> using namespace std; class Class { public: Class() {}; void fun(Class const &other) { cout << "fun: Class const &\n"; gun(other); } void fun(Class &other) { cout << "fun: Class &\n"; gun(other); } void fun(Class &&tmp) { cout << "fun: Class &&\n"; gun(tmp); } void gun(Class const &other) { cout << "gun: Class const &\n"; } void gun(Class &other) { cout << "gun: Class &\n"; } void gun(Class &&tmp) { cout << "gun: Class &&\n"; } }; int main() { Class c1;;; Class const c0;; }

Generally it is pointless to define a function having an rvalue reference return type. The compiler decides whether or not to use an overloaded member expecting an rvalue reference on the basis of the provided argument. If it is an anonymous temporary it calls the function defining the rvalue reference parameter, if such a function is available. An rvalue reference return type is used, e.g., with the call, to keep the rvalue reference nature of its argument, which is known to be a temporary anonymous object. Such a situation can be exploited also in a situation where a temporary object is passed to (and returned from) a function which must be able to modify the temporary object. The alternative, passing a , is less attractive as it requires a before the object can be modified. Here is an example:

std::string &&doubleString(std::string &&tmp) { tmp += tmp; return std::move(tmp); } This allows us to do something like std::cout << doubleString(std::string("hello ")); to insert into .

The compiler, when selecting a function to call applies a fairly simple algorithm, and also considers copy elision. This is covered shortly (section 9.8).

9.7.1: The move constructor (dynamic data)

Our class has, among other members, a data member . Clearly, should define a copy constructor, a destructor and an overloaded assignment operator.

Now consider the following function extracting the strings for a object from . Next, the object filled by is returned by value. The function returns a temporary object, which can then used to initialize an external object:

Strings loadStrings(std::istream &in) { Strings ret; // load the strings into 'ret' return ret; } // usage: Strings store(loadStrings(cin)); In this example two full copies of a object are required:
  • initializing 's value return type from its local object;
  • initializing from 's return value
We can improve the above procedure by defining a move constructor. Here is the declaration of the class move constructor: Strings(Strings &&tmp);

Move constructors of classes using dynamic memory allocation are allowed to assign the values of pointer data members to their own pointer data members without requiring them to make a copy of the source's data. Next, the temporary's pointer value is set to zero to prevent its destructor from destroying data now owned by the just constructed object. The move constructor has grabbed or stolen the data from the temporary object. This is OK as the temporary object cannot be referred to again (as it is anonymous, it cannot be accessed by other code) and the temporary objects cease to exist shortly after the constructor's call. Here is the implementation of move constructor:

Strings::Strings(Strings &&tmp) : d_string(tmp.d_string), d_size(tmp.d_size), d_capacity(tmp.d_capacity) { tmp.d_string = 0; }

In section 9.5 it was stated that the copy constructor is almost always available. Almost always as the declaration of a move constructor suppresses the default availability of the copy constructor. The default copy constructor is also suppressed if a move assignment operator is declared (cf. section 9.7.3).

The following example shows a simple class , declaring a move constructor. In the function following the class interface a object is defined which is then passed to the constructor of a second object. Compilation fails with the compiler reporting:

error: cannot bind 'Class' lvalue to 'Class&&' error: initializing argument 1 of 'Class::Class(Class&&)' class Class { public: Class() = default; Class(Class &&tmp) {} }; int main() { Class one; Class two{ one }; }

The cure is easy: after declaring a (possibly ) copy constructor the error disappears:

class Class { public: Class() = default; Class(Class const &other) = default; Class(Class &&tmp) {} }; int main() { Class one; Class two{ one }; }

9.7.2: The move constructor (composition)

Classes not using pointer members pointing to memory controlled by its objects (and not having base classes doing so, see chapter 13) may also benefit from overloaded members expecting rvalue references. The class benefits from move operations when one or more of the composed data members themselves support move operations.

Move operations cannot be implemented if the class type of a composed data member does not support moving or copying. Currently, classes fall into this category.

An example of a move-aware class is the class . A class could use composition by defining and . Its move constructor would then have the following prototype:

Person(Person &&tmp);

However, the following implementation of this move constructor is incorrect:

Person::Person(Person &&tmp) : d_name(tmp.d_name), d_address(tmp.d_address) {} It is incorrect as 's copy constructors rather than 's move constructors are called. If you're wondering why this happens then remember that move operations are only performed for anonymous objects. To the compiler anything having a name isn't anonymous. And so, by implication, having available a rvalue reference does not mean that we're referring to an anonymous object. But we know that the move constructor is only called for anonymous arguments. To use the corresponding move operations we have to inform the compiler that we're talking about anonymous data members as well. For this a cast could be used (e.g., ), but the C++-0x standard provides the function to anonymize a named object. The correct implementation of 's move construction is, therefore: Person::Person(Person &&tmp) : d_name( std::move(tmp.d_name) ), d_address( std::move(tmp.d_address) ) {} The function is (indirectly) declared by many header files. If no header is already declaring then include .

When a class using composition not only contains class type data members but also other types of data (pointers, references, primitive data types), then these other data types can be initialized as usual. Primitive data type members can simply be copied; references can be initialized as usual and pointers may use move operations as discussed in the previous section.

The compiler never calls move operations for variables having names. Let's consider the implications of this by looking at the next example, assuming offers a move constructor and a copy constructor:

Class factory(); void fun(Class const &other); // a void fun(Class &&tmp); // b void callee(Class &&tmp); { fun(tmp); // 1 } int main() { callee(factory()); }
  • At 1 function a is called. At first sight this might be surprising, but 's argument is not an anonymous temporary object but a named temporary object.
Realizing that might be called twice the compiler's choice is understandable. If 's data would have been grabbed at the first call, the second call would receive without any data. But at the last call we might know that is never used again and so we might like to ensure that is called. For this, once again, is used: fun(std::move(tmp)); // last call!

9.7.3: Move-assignment

In addition to the overloaded assignment operator a move assignment operator may be implemented for classes supporting move operations. In this case, if the class supports swapping the implementation is surprisingly simple. No copy construction is required and the move assignment operator can simply be implemented like this: Class &operator=(Class &&tmp) { swap(tmp); return *this; } If swapping is not supported then the assignment can be performed for each of the data members in turn, using as shown in the previous section with a class . Here is an example showing how to do this with that class : Person &operator=(Person &&tmp) { d_name = std::move(tmp.d_name); d_address = std::move(tmp.d_address); return *this; } As noted previously (section 9.7.1) declaring a move assignment operator suppresses the default availability of the copy constructor. It is made available again by declaring the copy constructor in the class's interface (and of course by providing an explicit implementation or by using the default implementation).

9.7.4: Revising the assignment operator (part II)

Now that we've familiarized ourselves with the overloaded assignment operator and the move-assignment, let's once again have a look at their implementations for a class , supporting swapping through its member. Here is the generic implementation of the overloaded assignment operator: Class &operator=(Class const &other) { Class tmp{ other }; swap(tmp); return *this; } and this is the move-assignment operator: Class &operator=(Class &&tmp) { swap(tmp); return *this; } They look remarkably similar in the sense that the overloaded assignment operator's code is identical to the move-assignment operator's code once a copy of the object is available. Since the overloaded assignment operator's object really is nothing but a temporary object we can use this fact by implementing the overloaded assignment operator in terms of the move-assignment. Here is a second revision of the overloaded assignment operator: Class &operator=(Class const &other) { Class tmp{ other }; return *this = std::move(tmp); }

9.7.5: Moving and the destructor

Once a class becomes a move-aware class one should realize that its destructor still performs its job as implemented. Consequently, when moving pointer values from a temporary source to a destination the move constructor should make sure that the temporary's pointer value is set to zero, to prevent doubly freeing memory.

If a class defines pointers to pointer data members there usually is not only a pointer that is moved, but also a defining the number of elements in the array of pointers.

Once again, consider the class . Its destructor is implemented like this:

Strings::~Strings() { for (string **end = d_string + d_size; end-- != d_string; ) delete *end; delete[] d_string; } The move constructor (and other move operations!) must realize that the destructor not only deletes , but also considers . A member implementing move operations should therefore not only set to zero but also . The previously shown move constructor for is therefore incorrect. Its improved implementation is: Strings::Strings(Strings &&tmp) : d_string(tmp.d_string), d_size(tmp.d_size), d_capacity(tmp.d_capacity) { tmp.d_string = 0; tmp.d_size = 0; } If operations by the destructor all depend on having a non-zero value then variations of the above approach are possible. The move operations could merely set to 0, testing whether in the destructor (and if so, end the destructor's actions). In the latter variant the assignment can be omitted.

9.7.6: Move-only classes

Classes may very well allow move semantics without offering copy semantics. Most stream classes belong to this category. Extending their definition with move semantics greatly enhances their usability. Once move semantics becomes available for such classes, so called factory functions (functions returning an object constructed by the function) can easily be implemented. E.g., // assume char *filename ifstream inStream(openIstream(filename)); For this example to work an constructor must offer a move constructor. This ensures that only one object refers to the open .

Once classes offer move semantics their objects can also safely be stored in standard containers (cf. chapter 12). When such containers perform reallocations (e.g., when their sizes are enlarged) they use the object's move constructors rather than their copy constructors. As move-only classes suppress copy semantics containers storing objects of move-only classes implement the correct behavior in that it is impossible to assign such containers to each other.

9.7.7: Default move constructors and assignment operators

As we've seen, classes by default offer a copy constructor and assignment operator. These class members are implemented so as to provide basic support: data members of primitive data types are copied byte-by-byte, but for class type data members their corresponding copy constructors c.q. assignment operators are called. The compiler also attempts to provide default implementations for move constructors and move assignment operators. However, the default constructors and assignment operators cannot always be provided.

These are the rules the compiler applies when deciding what to provide or not to provide:

  • If the copy constructor or the copy assignment operator is declared, then the default move constructor and move assignment operator are suppressed; their use is replaced by the corresponding copy operation (constructor or assignment operator);
  • If the move constructor or the move assignment operator is declared then the copy constructor and the copy assignment operator are implicitly declared as deleted, and can therefore not be used anymore;
  • If either the move constructor or the move assignment operator is declared, then (in addition to suppressing the copy operations) the default implementation of the other move-member is also suppressed;
  • In all other cases the default copy and move constructors and the default copy and assignment operators are provided.

If default implementations of copy or move constructors or assignment operators are suppressed, but they should be available, then it's easy to provide the default implementations by specifying the required signatures, to which the specification `' is added.

Here is an example of a class offering all defaults: constructor, copy constructor, move constructor, copy assignment operator and move assignment operator:

class Defaults { int d_x; Mov d_mov; };

Assuming that is a class offering move operations in addition to the standard copy operations, then the following actions are performed on the destination's and :

Defaults factory(); int main() { Mov operation: d_x: --------------------------- Defaults one; Mov(), undefined Defaults two(one); Mov(Mov const &), one.d_x Defaults three(factory()); Mov(Mov &&tmp), tmp.d_x one = two; Mov::operator=( two.d_x Mov const &), one = factory(); Mov::operator=( tmp.d_x Mov &&tmp) }

If, declares at least one constructor (not being the copy- or move constructor) as well as the copy assignment operators then only the default copy- and declared assignment operator are available. E.g.:

class Defaults { int d_x; Mov d_mov; public: Defaults(int x); Default &operator=(Default const &rhs); }; Defaults factory(); int main() { Mov operation: resulting d_x: -------------------------------- Defaults one; ERROR: not available Defaults two(one); Mov(Mov const &), one.d_x Defaults three(factory()); Mov(Mov const &), one.d_x one = two; Mov::operatpr=( two.d_x Mov const &) one = factory(); Mov::operator=( tmp.d_x Mov const &) } To reestablish the defaults, append to the appropriate declarations: class Defaults { int d_x; Mov d_mov; public: Defaults() = default; Defaults(int x); // Default(Default const &) remains available (by default) Defaults(Defaults &&tmp) = default; Defaults operator=(Defaults const &rhs); Defaults operator=(Defaults &&tmp) = default; };

The correct title of this article is C# syntax. The substitution or omission of the # is due to technical restrictions.

Main article: C Sharp (programming language)

This article describes the syntax of the C#programming language. The features described are compatible with .NET Framework and Mono.



An identifier is the name of an element in the code. There are certain standard naming conventions to follow when selecting names for elements.

An identifier can:

  • start with an underscore: _
  • contain an underscore: _
  • contain a numeral: 0123456789
  • contain both upper case and lower case Unicode letters. Case is sensitive (FOO is different from foo).

An identifier cannot:

  • start with a numeral
  • start with a symbol, unless it is a keyword (check Keywords)
  • contain more than 511 characters
  • contain @ sign in between or at the end


Keywords are predefined reserved words with special syntactic meaning. The language has two types of keyword — contextual and reserved. The reserved keywords such as or may only be used as keywords. The contextual keywords such as or are only treated as keywords in certain situations.[1] If an identifier is needed which would be the same as a reserved keyword, it may be prefixed by the @ character to distinguish it. This facilitates reuse of .NET code written in other languages.[2]

C# keywords, reserved words
1, 2 These are not actually keywords, thus (unlike actual keywords) it is possible to define variables and types using these names, but they act like keywords in certain new language constructs introduced in C# 2.0(1) and 3.0(2).

Using a keyword as an identifier:

string@out;// @out is an ordinary identifier, distinct from the 'out' keyword,// which retains its special meaning


Floating-point values
Characters escapes in strings
Unicode character followed by the hexadecimal unicode code point
Null character1
Carriage return
Form feed
Single quote
Double quote
Line feed
1Strings in C# are not null terminated

Digit separators[edit]

This is a feature of C# 7.0.

The underscore symbol separates digits in number values for readability purposes. Compiler will ignore it.


Generally, it may be put only between digit characters. It cannot be put at the beginning () or the end of the value ( or ), next to the decimal in floating point values (), next to the exponent character () and next to the type specifier ().


Variables are identifiers associated with values. They are declared by writing the variable's type and name, and are optionally initialized in the same statement.


intmyInt;// Declaring an uninitialized variable called 'myInt', of type 'int'


intmyInt;// Declaring an uninitialized variablemyInt=35;// Assigning the variable a value


intmyInt=35;// Declaring and initializing the variable

Multiple variables of the same type can be declared and initialized in one statement.

inta,b;// Declaring multiple variables of the same typeinta=2,b=3;// Declaring and initializing multiple variables of the same type

Local variable type inference[edit]

This is a feature of C# 3.0.

C# 3.0 introduced type inference, allowing the type specifier of a variable declaration to be replaced by the keyword , if its actual type can be statically determined from the initializer. This reduces repetition, especially for types with multiple generic type-parameters, and adheres more closely to the DRY principle.

varmyChars=newchar[]{'A','Ö'};// or char[] myChars = new char[] {'A', 'Ö'};varmyNums=newList<int>();// or List<int> myNums = new List<int>();

See also


Constants are immutable values.


When declaring a local variable or a field with the keyword as a prefix the value must be given when it is declared. After that it is locked and cannot change. They can either be declared in the context as a field or a local variable. Constants are implicitly static.

This shows all the uses of the keyword.



The keyword does a similar thing to fields. Like fields marked as they cannot change once initialized. The difference is that you can choose to initialize them in a constructor. This only works on fields. Read-only fields can either be members of an instance or static class members.

Code blocks[edit]

The operators are used to signify a code block and a new scope. Class members and the body of a method are examples of what can live inside these braces in various contexts.

Inside of method bodies you can use the braces to create new scopes like so:

voiddoSomething(){inta;{intb;a=1;}a=2;b=3;// Will fail because the variable is declared in an inner scope.}

Program structure[edit]

A C# application consists of classes and their members. Classes and other types exist in namespaces but can also be nested inside other classes.


Whether it is a console or a graphical interface application, the program must have an entry point of some sort. The entry point of the C# application is the method. There can only be one, and it is a static method in a class. The method usually returns and is passed command-line arguments as an array of strings.

staticvoidMain(string[]args){}// OR Main method can be defined without parameters.staticvoidMain(){}

A method is also allowed to return an integer value if specified.



Namespaces are a part of a type name and they are used to group and/or distinguish named entities from other ones.

System.IO.DirectoryInfo// DirectoryInfo is in the System.IO-namespace

A namespace is defined like this:

namespaceFooNamespace{// Members}


The statement loads a specific namespace from a referenced assembly. It is usually placed in the top (or header) of a code file but it can be placed elsewhere if wanted, e.g. inside classes.


The statement can also be used to define another name for an existing namespace or type. This is sometimes useful when names are too long and less readable.



Operator categoryOperators
Arithmetic, , , ,
Logical (boolean and bitwise), , , , , , , ,
String concatenation
Increment, decrement,
Relational (conditional), , , , ,
Assignment, , , , , , , , , ,
Member access
Delegate concatenation and removal,
Object creation
Type information, , ,
Overflow exception control,
Indirection and Address, , ,
Lambda expression

Operator overloading[edit]

Some of the existing operators can be overloaded by writing an overload method.


These are the overloadable operators:

, , , , , , , Unary operators
, , , , , , , , , Binary operators
, , , , , Comparison operators, must be overloaded in pairs
  • Assignment operators ( etc.) are combinations of a binary operator and the assignment operator () and will be evaluated using the ordinary operators, which can be overloaded.
  • Cast operators () cannot be overloaded, but you can define conversion operators.
  • Array indexing () operator is not overloadable, but you can define new indexers.

See also

Conversion operators[edit]

The cast operator is not overloadable but you can write a conversion operator method which lives in the target class. Conversion methods can define two varieties of operators, implicit and explicit conversion operators. The implicit operator will cast without specifying with the cast operator () and the explicit operator requires it to be used.

Implicit conversion operator

classFoo{publicintValue;publicstaticimplicitoperatorFoo(intvalue){returnnewFoo(value);}}// Implicit conversionFoofoo=2;

Explicit conversion operator

classFoo{publicintValue;publicstaticexplicitoperatorFoo(intvalue){returnnewFoo(value);}}// Explicit conversionFoofoo=(Foo)2;


The operator will attempt to do a silent cast to a given type. If it succeeds it will return the object as the new type, if it fails it will return a null reference.

Streamstream=File.Open(@"C:\Temp\data.dat");FileStreamfstream=streamasFileStream;// Will return an object.Stringstr=streamasString;// Will fail and return null.

Null coalesce operator[edit]

This is a feature of C# 2.0.

The following:


is shorthand for:


Meaning that if the content of variable is not null, that content will be returned, otherwise the content of variable is returned.

Control structures[edit]

C# inherits most of the control structures of C/C++ and also adds new ones like the statement.

Conditional structures[edit]

These structures control the flow of the program through given conditions.


The statement is entered when the given condition is true. Single-line case statements do not require block braces although it is mostly preferred by convention.

Simple one-line statement:

Multi-line with else-block (without any braces):

Recommended coding conventions for an if-statement.



The construct serves as a filter for different values. Each value leads to a "case". It is not allowed to fall through case sections and therefore the keyword is typically used to end a case. An unconditional in a case section can also be used to end a case. See also how statement can be used to fall through from one case to the next. Many cases may lead to the same code though. The default case handles all the other cases not handled by the construct.

switch(ch){case'A':statement;...break;case'B':statement;break;case'C':// A switch section can have multiple case'D':...break;default:...break;}

Iteration structures[edit]

Iteration statements are statements that are repeatedly executed when a given condition is evaluated as true.






The loop consists of three parts: declaration, condition and increment. Any of them can be left out as they are optional.


Is equivalent to this code represented with a statement, except here the variable is not local to the loop.



The statement is derived from the statement and makes use of a certain pattern described in C#'s language specification in order to obtain and use an enumerator of elements to iterate over.

Each item in the given collection will be returned and reachable in the context of the code block. When the block has been executed the next item will be returned until there are no items remaining.


Jump statements[edit]

Jump statements are inherited from C/C++ and ultimately assembly languages through it. They simply represent the jump-instructions of an assembly language that controls the flow of a program.

Labels and statement[edit]

Labels are given points in code that can be jumped to by using the statement.


The statement can be used in statements to jump from one case to another or to fall through from one case to the next.

switch(n){case1:Console.WriteLine("Case 1");break;case2:Console.WriteLine("Case 2");gotocase1;case3:Console.WriteLine("Case 3");case4:// Compilation will fail here as cases cannot fall through in C#.Console.WriteLine("Case 4");gotodefault;// This is the correct way to fall through to the next case.default:Console.WriteLine("Default");}


The statement breaks out of the closest loop or statement. Execution continues in the statement after the terminated statement, if any.

inte=10;for(inti=0;i<e;i++){while(true){break;}// Will break to this point.}


The statement discontinues the current iteration of the current control statement and begins the next iteration.

intch;while((ch=Console.Read())!=-1){if(ch==' ')continue;// Skips the rest of the while-loop// Rest of the while-loop...}

The loop in the code above reads characters by calling , skipping the statements in the body of the loop if the characters are spaces.

Exception handling[edit]

Runtime exception handling method in C# is inherited from Java and C++.

The base class library has a class called from which all other exception classes are derived. An -object contains all the information about a specific exception and also the inner exceptions that were caused. Programmers may define their own exceptions by deriving from the class.

An exception can be thrown this way:



Exceptions are managed within blocks.

try{// Statements which may throw exceptions...}catch(Exceptionex){// Exception caught and handled here...}finally{// Statements always executed after the try/catch blocks...}

The statements within the block are executed, and if any of them throws an exception, execution of the block is discontinued and the exception is handled by the block. There may be multiple blocks, in which case the first block with an exception variable whose type matches the type of the thrown exception is executed.

If no block matches the type of the thrown exception, the execution of the outer block (or method) containing the statement is discontinued, and the exception is passed up and outside the containing block or method. The exception is propagated upwards through the call stack until a matching block is found within one of the currently active methods. If the exception propagates all the way up to the top-most method without a matching block being found, the entire program is terminated and a textual description of the exception is written to the standard output stream.

The statements within the block are always executed after the and blocks, whether or not an exception was thrown. Such blocks are useful for providing clean-up code.

Either a block, a block, or both, must follow the block.


C# is a statically typed language like C and C++. That means that every variable and constant gets a fixed type when it is being declared. There are two kinds of types: value types and reference types.

Value types[edit]

Instances of value types reside on the stack, i.e. they are bound to their variables. If you declare a variable for a value type the memory gets allocated directly. If the variable gets out of scope the object is destroyed with it.


Structures are more commonly known as structs. Structs are user-defined value types that are declared using the keyword. They are very similar to classes but are more suitable for lightweight types. Some important syntactical differences between a and a are presented later in this article.

The primitive data types are all structs.

Pre-defined types[edit]

These are the primitive datatypes.

Primitive types
Type nameBCL equivalentValueRangeSizeDefault value
integer−128 through +1278-bit (1-byte)
integer−32,768 through +32,76716-bit (2-byte)
integer−2,147,483,648 through +2,147,483,64732-bit (4-byte)
integer−9,223,372,036,854,775,808 through
64-bit (8-byte)
unsigned integer0 through 2558-bit (1-byte)
unsigned integer0 through 65,53516-bit (2-byte)
unsigned integer0 through 4,294,967,29532-bit (4-byte)
unsigned integer0 through 18,446,744,073,709,551,61564-bit (8-byte)
signed decimal number−79,228,162,514,264,337,593,543,950,335 through
128-bit (16-byte)
floating point number±1.401298E−45 through ±3.402823E+3832-bit (4-byte)
floating point number±4.94065645841246E−324 through
64-bit (8-byte)
Boolean or 8-bit (1-byte)
single Unicode character through 16-bit (2-byte)

Note: () is not a struct and is not a primitive type.


Enumerated types () are named values representing integer values.

enumSeason{Winter=0,Spring=1,Summer=2,Autumn=3,Fall=Autumn// Autumn is called Fall in American English.}

variables are initialized by default to zero. They can be assigned or initialized to the named values defined by the enumeration type.

One thought on “Call Assignment Operator Of Base Class For Anonymous Types

Leave a Reply

Your email address will not be published. Required fields are marked *