Implicit class member functions in C ++

Implicit member functions of a class in C ++: According to the wiki: http://en.wikipedia.org/wiki/Special_member_functions

Default constructor (unless any other constructor is explicitly declared)

Copy constructor if no move constructor or redirect operator is explicitly declared. If a destructor is declared, then copy constructor generation is deprecated.

Move constructor if no copy constructor, assignment operator, or destructor is explicitly declared.

A copy assignment operator , unless a move constructor or move assignment operator is explicitly declared. If a destructor is declared, generating the copy assignment statement is deprecated.

Move assignment operator unless a copy constructor, copy assignment operator, or destructor is explicitly declared.

Destructor

As stated below:

http://archives.cs.iastate.edu/documents/disk0/00/00/02/43/00000243-02/lcpp_136.html

A default constructor (that is, one with no parameters (section 12.1 [Ellis-Stroustrup90]) if no constructor (with any number of arguments) for the class has been declared.

Copy constructor (Section 12.1 [Ellis-Stroustrup90]) if no copy constructor has been declared.

Destructor (Section 12.4 [Ellis-Stroustrup90]) if no destructor has been declared.

An assignment operator (sections 5.17 and 12.8 of [Ellis-Stroustrup90]), if no assignment operator has been declared.

As stated below:

http://www.picksourcecode.com/ps/ct/16515.php

default constructor

copy constructor

assignment operator

default destructor

address operator

Can anyone provide code examples for: Move Constructor, Copy Assignment Operator, Move Assignment Operator, Assign Operator, Address Operator where they are used as implicit member functions and are not explicitly defined.

thank

+3


source to share


1 answer


#include <iostream>

/* Empty struct. No function explicitly defined. */    
struct Elem
{ };

/* Factory method that returns an rvalue of type Elem. */
Elem make_elem()
{ return Elem(); }


int main() {

  /* Use implicit move constructor (the move may be elided
     by the compiler, but the compiler won't compile this if
     you explicitly delete the move constructor): */
  Elem e1 = make_elem();

  /* Use copy assignment: */
  Elem e2;
  e2 = e1;

  /* Use move assignment: */    
  e2 = make_elem();

  /* Use address operator: */    
  std::cout << "e2 is located at " << &e2 << std::endl;

  return 0;
}

      



The above example uses an empty class. You can fill it with data members that have real move semantics (i.e. members where move is really different from copy), for example. a std::vector

, and you get move semantics automatically without defining move constructors or move assignment operators specifically for your class.

+3


source







All Articles