问题
I do not understand because when you create an object of the "Users" class not the message is printed containing the constructor.
class users
{
public:
users();
private:
int i;
};
users::users ()
{
cout<<"hello world";
}
int main ()
{
users users1();
return 0;
}
回答1:
users users1();
doesn't declare an object of the users
class, it declares a function that takes no arguments and returns an object of the users
class. To declare an object, use:
users users1;
回答2:
class users
{
public:
users();
private:
int i;
};
users::users ()
{
cout<<"hello world";
}
int main ()
{
users users1; // either you use this
users* user2 = new users(); // or you do this
return 0;
}
This worked fine for me. see here
来源:https://stackoverflow.com/questions/27161295/constructor-does-not-run