I assume you are fairly new to C ++ and possibly even object oriented programming. So, firstly, I hope you understand what a class constructor is.
Most types will have some kind of constructor used for initialization. Even when it comes to int primitives or other types of variables, everything can be initialized2. So, when creating classes, you are prompted to provide an initializer called a constructor. Constructors may have parameters that may or may not be required. They can also be overloaded, which means that a class can have many constructors.
Since constructors are initializers, the call is not implicit to them: the class constructor will be called every time you create an object of this class, whether using the new keyword or by declaring it on the stack:
CMyObject obj;
They should now be declared inside your class. They are methods, after all ... so what should be his name? For example, Python takes a different approach and uses the __init__ keyword to do this; The C ++ developer decided that this would be the name of the class.
This makes sense, because in the end, having a member method with a class name can cause name conflicts (ambiguity) along the system. (Although this series of articles is about C #, it explains why using a name for a member of a specific area with the same name is bad)
² But sometimes they are not intended to reduce lead time.
Bruno brant
source share