Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are all the member-functions created by compiler for a class? Does that happen all the time?

What are all the member-functions created by compiler for a class? Does that happen all the time? like destructor. My concern is whether it is created for all the classes, and why is default constructor needed?

like image 677
Onnesh Avatar asked Sep 17 '10 09:09

Onnesh


People also ask

Which member functions are created automatically by the compiler if they are not included in the class definition?

In C++, the compiler automatically generates the default constructor, copy constructor, copy-assignment operator, and destructor for a type if it does not declare its own. These functions are known as the special member functions, and they are what make simple user-defined types in C++ behave like structures do in C.

What are member functions in a class?

Member functions are operators and functions that are declared as members of a class. Member functions do not include operators and functions declared with the friend specifier. These are called friends of a class. You can declare a member function as static ; this is called a static member function.


2 Answers

C++98/03

If they are needed,

  1. the compiler will generate a default constructor for you unless you declare any constructor of your own.
  2. the compiler will generate a copy constructor for you unless you declare your own.
  3. the compiler will generate a copy assignment operator for you unless you declare your own.
  4. the compiler will generate a destructor for you unless you declare your own.

As Péter said in a helpful comment, all those are only generated by the compiler when they are needed. (The difference is that, when the compiler cannot create them, that's Ok as long as they aren't used.)


C++11

C++11 adds the following rules, which are also true for C++14 (credits to towi, see this comment):

  • The compiler generates the move constructor if
    • there is no user-declared copy constructor, and
    • there is no user-declared copy assignment operator, and
    • there is no user-declared move assignment operator and
    • there is no user-declared destructor,
    • it is not marked deleted,
    • and all members and bases are moveable.
  • Similarly for move assignment operator, it is generated if
    • there is no user-declared copy constructor, and
    • there is no user-declared copy assignment operator, and
    • there is no user-declared move constructor and
    • there is no user-declared destructor,
    • it is not marked deleted,
    • and all members and bases are moveable.

Note that these rules are a bit more elaborate than the C++03 rules and make more sense in practice.

For an easier understanding of what is what in the above:

class Thing { public:     Thing();                        // default constructor     Thing(const Thing&);            // copy c'tor     Thing& operator=(const Thing&); // copy-assign     ~Thing();                       // d'tor     // C++11:     Thing(Thing&&);                 // move c'tor     Thing& operator=(Thing&&);      // move-assign }; 

Further reading: if you are a C++-beginner consider a design that does not require you to implement any of five a.k.a The Rule Of Zero originally from an article written by Martinho Fernandes.

like image 160
12 revs, 6 users 45% Avatar answered Sep 21 '22 04:09

12 revs, 6 users 45%


Do you mean 'defined' by 'created'?

$12.1 - "The default constructor (12.1), copy constructor and copy assignment operator (12.8), and destructor (12.4) are special member functions.

If 'created' means 'defined' then, here are the important parts from the C++ Standard.

-An implicitly-declared default constructor for a class is implicitly defined when it is used to create an object of its class type (1.8).

-If a class has no user-declared destructor, a destructor is declared implicitly. An implicitly-declared destructor is implicitly defined when it is used to destroy an object of its class type.

-If the class definition does not explicitly declare a copy constructor, one is declared implicitly. An implicitly-declared copy constructor is implicitly defined if it is used to initialize an object of its class type from a copy of an object of its class type or of a class type derived from its class type).

-If the class definition does not explicitly declare a copy assignment operator, one is declared implicitly. An implicitly-declared copy assignment operator is implicitly defined when an object of its class type is assigned a value of its class type or a value of a class type derived from its class type.

like image 21
Chubsdad Avatar answered Sep 25 '22 04:09

Chubsdad