C ++类的默认构造函数 [英] C++ Classes default constructor

查看:210
本文介绍了C ++类的默认构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前我问为什么这被认为是坏的:

Earlier I asked why this is considered bad:

class Example
{
 public: 
  Example(void);
  ~Example(void);
  void f() {}
}

int main(void)
{
 Example ex(); // <<<<<< what is it called to call it like this?

 return(0);
}

现在,我理解它创建一个函数原型, 。我仍然不明白为什么它将工作在g ++和MS VC ++虽然。

Now, I understand that it's creating a function prototype instead that returns a type Example. I still don't get why it would work in g++ and MS VC++ though.

我的下一个问题是使用上面的,这个调用是否有效

My next question is using the above, would this call be valid?

int main(void)
{
 Example *e = new Example();

 return(0);
}

?它和之间的区别是什么,简单地调用Example e()???像我知道这是一个函数原型,但它似乎也许一些编译器原谅,并允许它调用默认构造函数?我也尝试过:

? What is the difference between that and simply calling Example e()??? Like I know it's a function prototype, but it appears maybe some compilers forgive that and allow it to call the default constructor? I tried this too:

class Example
{
 private:
  Example();

 public:
  ~Example();
};

int main(void)
{
 Example e1(); // this works
 Example *e1 = new Example(); // this doesn't

 return(0);
}

所以我有点困惑:(对不起,

So I'm a bit confused :( Sorry if this been asked a million times.

推荐答案

问题将有助于了解此行为

这篇关于C ++类的默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆