I was doing some experiment today with the constructors:
class cls
{
int a;
public:
cls(){cout<<"Default constructor called\n";}
cls(int b){a=b;cout<<"Constructor with parameter called";}
}
Then this kind of initialization
cls x=5;
yields an output saying that the constructor with parameter has been called.
My question i: what if I have a constructor with two or more parameters? Can I still use the initialization by assignment?
Foo x = {blah, blah};.-std=c++11to the compiler?cls(int b){a=b;cout<<"Constructor with parameter called";}-->cls(int b) : a(b) {cout<<"Constructor with parameter called";}- you want to initializeain the initialization list, not default initialize it first and then subsequently assign to it in the constructor body. That's wasteful and won't even work for types that cannot be default constructed or cannot be assigned to.