Why we do need constructors? [closed]

北城以北 提交于 2021-02-16 13:58:06

问题


Today my friend asked me, why really do we need constructors in C++? Where as we can do the same in structural language. What is the specialty of constructors, show me the need of constructors so that I should use it in my C++ program. Please help me and give me some examples so that I could able to clear his doubt.


回答1:


You don't need constructors in the same sense that you don't need most features of popular languages.

Constructors exist to make it more difficult to do The Wrong Thing. In this case, using data that hasn't been initialized.




回答2:


A pathological answer is that constructors do not change the Turing completeness of the language, so, in a strict sense, you don't need them, as you don't need many features of the language. But this is formal rather than practical. You will be excused for not feeling warmer at night thinking only of theory.

A good practical example for why constructors are useful is to think of the RAII pattern. By having a constructor, you encapsulate very nicely both the initialization and acquisition in the same place that you have destruction. C, which doesn't have constructors, is famous for programmers forgetting a step in that process.




回答3:


As other people already answered, you use constructor in the same way as you would use an init function in a procedural language, but thanks to constructors there is no way the programmer will forget to call the init function - the compiler does it for him. One additional benefit this gives, aside from taking care of calling it automatically, is gracefully resolving the inheritance initialization problem: in case of a class A that extends class B that extends class C, you are guaranteed that all three constructors (for class A, B and C) will be called and that they will be called in the correct order (class C, then class B, then class A) so that each constructor can already use all the data from the superclass (since it has already been initialized). In the case of a language without constructors, the programmer would need to take care of all this bookkeeping.




回答4:


It's just the way object-oriented languages work. In C, you would use malloc to allocate memory and then initialize that memory in some way. In C++, the constructor does both things. By putting these 2 things together, this makes it harder for the developer to allocate memory and fail/forget to initialize it.




回答5:


Because some objects need data to initialise. With constructors you can ensure at compile-time that the object gets the data. Otherwise the compiler would throw an error.




回答6:


Immutable objects.

When you are working with parallel or concurrent programming it is much easier to share objects that cannot be altered. You don't have to worry about race conditions, locks, etc. But the only way to create an immutable object in most OOP languages is via a constructor. You can't set properties on the object because, by definition, all of the properties are read-only.




回答7:


In a procedural language you need to initialize variables to a well defined value too. Typically with some kind of Init function. And a constructor is a way to ensure that when you create an object it's initialized to a valid state.

In .net you can bypass constructors(With privileged code), and you'll get an objects whose fields are initialized to 0.



来源:https://stackoverflow.com/questions/8749248/why-we-do-need-constructors

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