constructor does not run

允我心安 提交于 2019-12-31 07:34:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!