In which cases there is no constructor at all, even a default constructor?

爱⌒轻易说出口 提交于 2019-12-21 11:22:20

问题


In this book I am currently reading I ran across this:

A class doesn't need a constructor. A default constructor is not needed if the object doesn't need initialization.

Am I correct in inferring from the above that the compiler does not generate a default constructor for the class/structure in some cases? If yes, what are those cases? I will venture and say POD is probably one. Are there any other?

EDIT: I have changed the title as the original title gave the meaning that I asked when was a default constructor not defined instead of asking when does a class not have a constructor at all.


回答1:


A class doesn't need a constructor. A default constructor is not needed if the object doesn't need initialization.

I think the author is talking about this situation:

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

Under some circumstances a constructor won't be called, period. As soon as you write a constructor you don't have a POD class, so now the constructor will be called.

Whether it is a good or bad thing to have an object running around that contains random, uninitialized data is a different question entirely.




回答2:


A default constructor is always declared. But it is not always defined. 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

Note that this has direct practical consequences

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



回答3:


If you always create objects of an class using a constructor with parameters it won't need the default constructor.

The compiler generates a default constructor for every class, but if you define your own constructor for that class then the compiler does not generate a default constructor by itself. As long as you create objects of such an class through the constructor you provided, the class won't need and have 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 context of the above example, consider the quote from the book:

A class doesn't need a constructor. A default constructor is not needed if the object doesn't need initialization.

In the above example as long as the object of Myclass is created in using #1, the class does not require and have a default constructor.

The default constructor needs to be defined for the class, if object of Myclass is created in a way which needs the default constructor, i.e: #2.




回答4:


In my opinion, that sentence means that you don't always have to write your own default constructor, since some classes may not need to be initialized by default.

For example, if your class contains several class fields that provide their own default constructor you don't need to write any default constructor, since the members' constructor is called anyway by default.

At the extreme opposite, you may want to write a struct or a class of PODs for which you rely on the programmer to initialize correctly its fields manually; in this case, you may not write a default constructor, so the compiler will write its own that will leave these fields to their default uninitialized values (actually, it will be a no-op, and will probably be optimized away).




回答5:


The compiler only declares and defines an automatically generated default constructor if you haven't provided any constructor.

With a non-instantiable parent class however, it is possible to prevent any kind of constructor from working. By adding a dummy constructor with a dummy parameter, it is possible to kill only the automatically generated default constructor, at the expense of more red tape.




回答6:


There's certain ambiguity in your question. You see, the implicit actions that the compiler takes with regard to constructors involve both declaring them and defining them. If some constructor is declared but not defined, do you consider it exists or not?

Anyway, there's no way to create a class that has no constructors declared for it. The copy constructor, for one example, is always declared. There's no way to suppress it. If you don't declare it yourself, the compiler will declare it for you.

As for the default constructor - it is possible to suppress its implicit declaration. If you declare any constructor yourself (i.e. explicitly), the compiler will not implicitly declare the default one. But in this case your class will, of course, have a constructor: the one that you declared yourself. (Plus, 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 defined only if it is possible. (If you use an implicit constructor, and it proves to be impossible to define, then your program simply will not compile).

So, once again, when it comes to declared constructors, it is not possible to have a class with no constructors at all. Any class has at least one constructor declared for it.

If your are interested in defined constructors specifically, then it is indeed possible to have a class, for which no constructor is defined. Here's an example for you

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

That's it. The class has one constructor declared it in, but it is not defined :)




回答7:


A default constructor is not defined for a class if another constructor is declared.

For POD types (in the sense of being both trivial and standard-layout, as those terms are defined in C++11) it is a moot point whether the compiler generates a constructor or not, since the compiler-generated constructors are trivial. For gory details, have a look at What are Aggregates and PODs and how/why are they special?




回答8:


So simply put - (in context of CPP) If not Constructor is defined, then compiler doesnt have default constructor. It is defined by the compiler only if it is required.

There are certain cases where it is being done by the compiler. Some of which are -

  1. When we have a class which has a base class object as member( and derived class constructor is not defined). In this case the default contructor for derived class is created because the prolog of its contructor needs to call the base class contructor.
  2. We have a container object. Below code explains this.

    class Legs
    {
    ctor(); // ctor stands for constructor
    };
    class cat
    {
    Legs leg;
    public:
    cat(){}
    };
    
  3. In case of Virtual functions, the setting of Virtual table pointer to the correct V-Table is done in Constructor. For this reason also, the default constructor will be defined by the compiler.




回答9:


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



来源:https://stackoverflow.com/questions/7131983/in-which-cases-there-is-no-constructor-at-all-even-a-default-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!