Answer the question
In order to leave comments, you need to log in
What happens to an object if its constructors are called multiple times?
Hello
//source.cpp
CClass::CClass(){} //Первый конструктор
CClass::CClass(int a){} //Второй конструктор
//main.cpp
CClass obj; //вызываем первый конструктор
obj = 1; //вызываем второй конструктор
Answer the question
In order to leave comments, you need to log in
The second entry is not a constructor call, but an assignment operator.
First, a new temporary object is created through the second constructor (because the constructor is implicit, an implicit conversion from int to CClass is allowed). The assignment operator is then called on the first object with the temporary object as a parameter.
In C++, without complex manipulations, it is impossible to call a constructor a second time.
In fact your code is equivalent to this:
CClass obj(); // на самом деле так писать нельзя, это не вызов конструктора а объявление функции
obj = CClass(1);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question