Is it true that the default constructor is synthesized for every class that does not define it?

If the class does not have a constructor, will the compiler make one default constructor for it?

Programmers new to C ++ often have two common misunderstandings:

What is the default constructor synthesized for each class that does not define one

from the book Inside the C ++ Object Model

I am at a loss ...

+5
source share
6 answers

This is well explained in the section from which this quote is taken. I will not rephrase it in its entirety, but here is a brief overview of the contents of the section.

, : implicitly-declared, implicitly-defined, trivial, non-trivial synthesized (, , ).

implicitly-declared , user-declared. , struct T { }; , . , struct T { T(int); }; , . T , .

implicitly-declared - implicitly-defined, , .. . struct T { };, T t; T::T(). , , . - ! ( , - ) .

implicitly-declared trivial, :

  • trivial
  • trivial.

, . ,

struct Trivial
{
    int i;
    char * pc;
};

int main()
{
    Trivial t;
}

T ( , : T).

, , , implicitly-declared non-trivial, , , , , synthesize , .

, :

struct NonTrivial
{
    virtual void foo();
};

-, ( , , ).

,

struct NonTrivial
{
    std::string s;
};

, trivial. . , , NonTrivial n; ( , ).


, . , , ( , , , ). - . ( ), , , , .


N.B.

Stanley B Lippman " ++", () ++, . , : , . ++ , , - " /" ( / , (, )).

+12

, :

,

, , , , .

, - , .

class MyClass {
public:
    MyClass(int x) {}; // No default constructor will be generated now
};

, , MyClass :

MyClass mc;

, , .

, OP .

, MyClass :

class MyClass {
};

int main() {
    MyClass m;
}

, MyClass(), MyClass.

:

#include <iostream>

class MyClass {

};

int main() {
    std::cout << "exiting\n";
}

, , MyClass .

:

#include <iostream>

class MyClass {
public:
    MyClass(int x = 5) { _x = x; }
    int _x;
};

int main() {
    MyClass m;
    std::cout << m._x;
}

MyClass(), , . , MyClass(int x = 5) , , .

, , :

class MyClass() {
public:
    MyClass(int x) { _x = x; }
    int _x;
};

int main() {
    MyClass m;
}

, MyClass m ( ), , int. .

+9

, , :

  • , , ,
  • , .
+4

, , :

, .

,

, .

, .

:

, , - .

:

struct NoDefaultConstructor
{
     NoDefaultConstructor(int);
};

class Surprise1
{
     NoDefaultConstructor m;
} s1; // fails, no default constructor exists for Surprise1

class Surprise1 , .

, :

class Surprise2 : public NoDefaultConstructor
{
} s2; // fails, no default constructor exists for Surprise2

, :

class NonPublicConstructor
{
protected:
    NonPublicConstructor();
};

class Surprise3
{
    NonPublicConstructor m;
} s3; // fails, no default constructor exists for Surprise3
+3

, (. ).

0

http://www.codeguru.com/forum/archive/index.php/t-257648.html

:

" ++", . , .

, , . , . . , - . , , , . , .... , ++, :

  • What the default constructor is synthesized for each class that does not define one

  • That the default compiler built by default contains explicit default initializers for each data item declared in the class

As you have seen, none of them is true.

0
source

All Articles