Why do constructors always have the same name and class, and how are they invoked implicitly?

I want to know why the constructor name is always the same as the class name, and how its invocation is called implicitly when creating an object of this class. Can someone explain the course of execution in such a situation?

+4
source share
7 answers

I want to know why the constructor name always matches the class name

Because this syntax does not require new keywords. In addition, there is no good reason.

To minimize the number of new keywords, I did not use explicit syntax like this:

class X { constructor(); destructor(); } 

Instead, I chose ad syntax that reflected the use of constructors.

 class X { X(); ~X(); 

Perhaps it was too smart. [Design and evolution of C ++, 3.11.2 Designator designation]


Can someone explain the progress in such a situation?

The lifetime of an object can be summarized as follows:

  • allocate memory
  • call constructor
  • use object
  • call destructor / finalizer
  • free memory

In Java, step 1 always allocates from the heap. In C #, classes are also allocated from the heap, while memory for structures is already available (either on the stack in case of unoccupied local structures, or in their parent / close object). Please note that knowledge of these details is usually not necessary or very useful . In C ++, memory allocation is extremely complicated, so I will not go into details here.

Step 5 depends on how the memory was allocated. The stack memory is automatically freed as soon as the method ends. In Java and C #, heap memory is implicitly released by the garbage collector at some unknown time after it is no longer needed. In C ++, heap memory is technically freed by calling delete . In modern C ++, delete rarely called manually. Instead, you should use RAII objects such as std::string , std::vector<T> and std::shared_ptr<T> , which will take care of this themselves.

+13
source

Why? Because the designers of the different languages โ€‹โ€‹that you mentioned decided to make them that way. It is possible that someone will develop an OOP language where the constructors should not have the same name as the class (as this is commented, this applies to python).

This is a simple way to distinguish constructors from other functions and makes building a class in code very readable, therefore it makes sense as a choice of language.

The mechanism is somewhat different in different languages, but essentially it is just a method call using language functions (for example, the new keyword in java and C #).

The constructor is run at run time whenever a new object is created.

+6
source

It seems to me that with the sepearte keywords for declaring the constructor (s) it will be โ€œbetterโ€, since it will remove an unnecessary dependency on the name of the class itself.

Then, for example, the code inside the class can be copied as the body of another without changing the name of the name (s) of the constructor. Why I would like to do this, I do not know (perhaps during some process of code refactoring), but the fact is that everyone always strives for independence between things, and here the syntax of the language goes against this, I think.

The same goes for destructors.

+4
source

One of the good reasons for a constructor of the same name is their expressiveness. For example, in Java you create an object such as

 MyClass obj = new MyClass(); // almost same in other languages too 

The constructor is now defined as <

 class MyClass { public MyClass () {... } } 

Thus, the above expresses very well that you are creating an object, and during this process the constructor MyClass() called.

Now, when you create an object, it always calls its constructor. If this extend class uses some other base class, then its constructor will be called first and so on. All these operations are implicit. First, memory is allocated for the object (on the heap), and then the constructor is called to initialize the object. If you do not provide a constructor, the compiler will generate it for your class.

+2
source

In C ++, strictly speaking, constructors have no names at all. 12.1 / 1 in standard states: "Constructors have no names", this does not become much clearer than this.

The syntax for declaring and defining constructors in C ++ uses the class name. There must be some way to do this, and using the class name is concise and easy to understand. C # and Java both copied the C ++ syntax, apparently because it would have been familiar with at least some of the audience they were targeting.

The exact flow of execution depends on what language you speak, but what you call three is that at first some memory is assigned from somewhere (maybe dynamically allocated, maybe it's a certain area of โ€‹โ€‹the stack memory or something yet). Then, the runtime is responsible for invoking the correct constructor or constructors in the correct order for the derived class itself, as well as the base classes. It depends on the implementation of how to do this, but the required effects are determined by each of these languages.

For the simplest possible case in a C ++ class that does not have base classes, the compiler simply issues a call to the constructor specified by the code that creates the object, i.e. a constructor that matches any arguments provided. This becomes more difficult if you have several virtual bases in the game.

+2
source

I want to know why the constructor name always matches the name of the class name

So that it can be uniquely identified as a constructor.

and how its call is invoked implicitly when we create an object of this class.

It is called by the compiler because it is already uniquely identified due to its name.

Can someone explain the progress in such a situation?

  • The new operator X () is called.
  • Memory is allocated or an exception is thrown.
  • The constructor is called.
  • The new () statement returns to the caller.

The question is why did the designers decide this?

Naming a constructor after its class is a long-established convention that dates back to at least the early days of C ++ in the early 1980s, possibly to the predecessor of Simula.

+1
source

This convention is for ease of programming, constructor chaining, and language consistency.

For example, consider a scenario in which you want to use the Scanner class, and now what if the JAVA developers called the constructor xyz!

Then how do you know what you need to write:

ScObj scanner = new xyz (System.in);

which could be really strange, right! Or, rather, you may have to go to a huge manual to check the constructor name of each class to get the created object, which again does not make sense if you could solve the problem by simply naming the constructors the same as class.

Secondly, the constructor itself is created by the compiler, if you do not provide it explicitly, then the best name for the constructor can be automatically selected by the compiler, so that the programmer can understand it! Obviously, the best choice is to leave it the same as the class.

Thirdly, you may have heard about the chain of constructors, and then when you call the chain between the constructors, how the compiler finds out what name you gave the constructor of the chain class! Obviously, the solution to the problem is the same again. KEEP THE DESIGNER NAME IS SAME AS A CLASS.


When you create an object, you call the constructor, calling it in your code using the new keyword (and passing arguments if necessary), then all the superclass constructors are called by creating a chain of calls that ultimately give the object.

Thanks for the question.

0
source

All Articles