Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why constructors will always have same name as of class and how they are invoked implicitly?

Tags:

java

c++

c#

I want to know that why the name of constructor is always same as that of class name and how its get invoked implicitly when we create object of that class. Can anyone please explain the flow of execution in such situation?

like image 955
Microsoft Developer Avatar asked Nov 27 '22 12:11

Microsoft Developer


2 Answers

I want to know that why the name of constructor is always same as that of class name

Because this syntax does not require any new keywords. Aside from that, there is no good reason.

To minimize the number of new keywords, I didn't use an explicit syntax like this:

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

Instead, I chose a declaration syntax that mirrored the use of constructors.

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

This may have been overly clever. [The Design And Evolution Of C++, 3.11.2 Constructor Notation]


Can anyone please explain the flow of execution in such situation?

The lifetime of an object can be summarized like this:

  1. allocate memory
  2. call constructor
  3. use object
  4. call destructor/finalizer
  5. release memory

In Java, step 1 always allocates from the heap. In C#, classes are allocated from the heap as well, whereas the memory for structs is already available (either on the stack in the case of non-captured local structs or within their parent object/closure). Note that knowing these details is generally not necessary or very helpful. In C++, memory allocation is extremely complicated, so I won't go into the details here.

Step 5 depends on how the memory was allocated. Stack memory is automatically released 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 released by calling delete. In modern C++, delete is rarely called manually. Instead, you should use RAII objects such as std::string, std::vector<T> and std::shared_ptr<T> that take care of that themselves.

like image 102
fredoverflow Avatar answered Dec 01 '22 00:12

fredoverflow


Why? Because the designers of the different languages you mention decided to make them that way. It is entirely possible for someone to design an OOP language where constructors do not have to have the same name as the class (as commented, this is the case in python).

It is a simple way to distinguish constructors from other functions and makes the constructing of a class in code very readable, so makes sense as a language design choice.

The mechanism is slightly different in the different languages, but essentially this is just a method call assisted by language features (the new keyword in java and c#, for example).

The constructor gets invoked by the runtime whenever a new object is created.

like image 41
Oded Avatar answered Nov 30 '22 23:11

Oded