When is there no constructor at all, not even a default constructor?

In this book I am currently reading that I came across this:

The class does not need a constructor. The default constructor is not needed if the object does not need initialization.

Are you correct from the above from the fact that the compiler in some cases does not generate a default constructor for a class / structure? If so, what are these cases? I will take a chance and say that the POD is probably one. Are there any others?

EDIT: I changed the title because the original name gave the value that I asked when I was the default constructor that was not set, instead of asking when the class had no constructor at all.

+6
source share
9 answers

The class does not need a constructor. The default constructor is not needed if the object does not need initialization.

I think the author talks about this situation:

some_type some_function () { POD_type this_is_intentionally_uninitialized; ... } 

In some cases, the constructor will not be called, period. Once you write the constructor, you do not have the POD class, so the constructor will now be called.

Good or bad is that an object that works around contains random, uninitialized data, this is a completely different matter.

+5
source

The default constructor is always declared. But this is not always determined. Only if it is used, then the compiler (or you) define it. Examples:

 struct A { std::string str; }; // not yet defined struct B : A { }; // not yet defined B b; // Now B::B and A::A are defined 

Please note that this has direct practical implications.

 struct A { private: A(); }; struct B : A { }; // valid, as B::B is not yet defined B b; // now invalid, because B::B is defined and tries to call a // private base class constructor 
+5
source

If you always create class objects using a constructor with parameters, it does not need a default constructor.

The compiler creates a default constructor for each class, but if you define your own constructor for this class, then the compiler itself does not create a default constructor. While you create objects of such a class through the constructor you provide, the class is not needed and has a default constructor.

 class Myclass { int m_i; public: Myclass(int i) { m_i = i; } }; int main() { Myclass obj1(10); // #1, uses overloaded constructor Myclass obj2; //#2, Will generate compiler error of no matching constructor return 0; } 

In the context of the above example, consider a quote from a book:

The class does not need a constructor. The default constructor is not needed if the object does not need initialization.

In the above example, while the Myclass object is created using # 1, the class does not require and has a default constructor.

The default constructor must be defined for the class if the Myclass object is created in a way that needs a default constructor, that is: # 2.

+3
source

In my opinion, this suggestion means that you do not always have to write your own default constructor, since by default some classes cannot be initialized.

For example, if your class contains several class fields that provide their own default constructor, you do not need to write a default constructor, since the element constructor is still called by default.

As a last resort, you can write a struct or class POD for which you rely on the programmer to correctly initialize your fields manually; in this case, you cannot write a default constructor, so the compiler will write its own, which will leave these fields with default uninitialized values ​​(in fact, it will be non-op and will probably be optimized).

0
source

The compiler only declares and defines an automatically created default constructor if you have not provided any constructor.

Together with the non-integrable parent class, you can prevent any constructor from working. By adding a dummy constructor with a dummy parameter, you can kill only the automatically created default constructor due to more bureaucracy.

0
source

There is a certain ambiguity in your question. You see that the implicit actions that the compiler uses with respect to constructors include both declaring them and defining them. If any constructor is declared but not defined, do you consider it existing or not?

In any case, there is no way to create a class for which constructors are not declared. A copy constructor, for example, is always declared. There is no way to crush him. If you do not declare it yourself, the compiler will declare it for you.

As for the default constructor, you can disable its implicit declaration. If you declare any constructor yourself (i.e. Explicitly), the compiler will not declare a default by default. But in this case, your class, of course, will have a constructor: one that you yourself declared. (Also, as I said above, the copy constructor is always declared).

As for implicitly defined constructors ... They are defined by the compiler only if you use them. And, of course, they are determined only if this is possible. (If you use an implicit constructor, and it is impossible to determine, then your program will simply not compile).

So again, when it comes to declared constructors, it is impossible to have a class without any constructors. Any class has at least one constructor declared for it.

If you are interested in specific constructors specifically, then it is really possible to have a class for which a constructor is not defined. Here is an example

 struct S { S(const S&); }; 

What is it. The class contains one constructor, but it is not defined :)

0
source

The default constructor is not defined for the class if another constructor is declared.

For POD types (in the sense of both trivial and standard layout, since these terms are defined in C ++ 11), the question is whether the compiler generates a constructor or not, since the constructors generated by the compiler are trivial. For details, see What are aggregates and PODs and how / why are they special?

0
source

So just put - (in the context of CPP) If no Constructor is defined, then the compiler does not have a default constructor. It is determined by the compiler only if necessary.

There are certain cases when this is done by the compiler. Some of them -

  • When we have a class that has an object of the base class as a member (and the constructor of the derived class is not defined). In this case, the default constructor for the derived class is created because the prolog of its constructor must call the constructor of the base class.
  • We have a container object. The following is an explanation of this code.

     class Legs { ctor(); // ctor stands for constructor }; class cat { Legs leg; public: cat(){} }; 
  • In the case of virtual functions, the virtual table pointer is set to the correct V-table in the constructor. For this reason, the default constructor will be determined by the compiler.

0
source

some_type some_function () {POD_type this_is_intentionally_uninitialized; ...}

0
source

Source: https://habr.com/ru/post/895488/


All Articles