views:

113

answers:

4

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?

+4  A: 
  1. The compiler generates a default constructor for you unless you declare any constructor of your own.
  2. The compiler generates a copy constructor for you unless you declare your own.
  3. The compiler generates an assignment operator for you unless you declare your own.
  4. The compiler generates 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 it isn't used.)

sbi
I'm not sure whether C++1x specifies any rules about compiler-generated cctors/assignops taking rvalue references.
sbi
For the sake of precision, all these are generated only when they are actually needed, not always. E.g. assignment operator is generated only if there is an actual assignment taking place to an instance of the class in question.
Péter Török
@sbi: It does. The rules are slightly more complex - from what I understand to make sure that C++03 style classes behave as expected. I'm not an expert on 0x but I understand that a move constructor isn't generated if the class has a user-declared copy constructor. You can declare one `= default` if you want the default implementation one back again.
Charles Bailey
@Charles: Although defaulting the move operations will probably have unintended semantics if the default copy operations do the wrong thing and thus had to be manually provided.
FredOverflow
@FredOverflow: Agreed. It would be a bizarre situation were a custom copy constructor was required but default move constructor worked fine.
Charles Bailey
@Péter: Thanks, I incorporated that into my answer.
sbi
+1  A: 

By default, if not implemented by the user, the compiler add some member functions to the class. Those are called the big four :

  • constructor
  • destructor
  • copy constructor
  • copy operator (assignment)

Depending on the types of the members and wich member function listed you provide yourself, those will not all be generated.

Klaim
A: 

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.

Chubsdad
A: 

Other answers have told you what's created, and that the compiler may only generate them if used.

My concern is whether it is created for all the classes...

Why concerned? Thinking it's creating unwanted code in the executable? Unlikely, but you can check easily enough with your environment.

Or perhaps your concern was that it might not create a constructor when you want one? Nothing to worry about... they're always created if needed and not provided by the user.

...and why is default constructor needed?

Because classes may have objects inside them with their own destructors that need to be systematically invoked. For example, given...

struct X
{
    std::string a;
    std::string b;
};

...the default destructor makes sure the destructors for a and b run.

Tony
No, the destructors for `b` and `a` will automatically run *after* running the empty `X` destructor.
FredOverflow
@Fred: From a user-code point of view, true. But the compiler is concatenating all explicit X destruction body (if provided) with the sub-object destructors to form the actual destructor function.
Tony