Hello i am new to learning C++. Is constructor created in the order i create it in the class or is it always called first before anything else is created in the class.
#include <iostream>
using namespace std;
class NonStatic
{
public:
int no = 2;
NonStatic():no(0) {
}
};
int main() {
NonStatic obj1;
cout << obj1.no;
return 0;
}
In this class. Will the constructor be created before the data member or after the data member
For
int no = 2;,nois initialized as2via default member initializer. ForNonStatic():no(0) {},nois initialized as0via member initializer list. Then the default member initializer is ignored, forNonStatic obj1;,obj1.nowill be initialized as0as the result.About the initialization order, data members are initialized firstly (via default member initializer or member initializer list), then constructor body is executed.