创建类的实例 [英] Creating an instance of class

查看:79
本文介绍了创建类的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第1,2,3,4行之间有什么区别?



什么时候使用?



为什么第3行打印构造函数Foo ,第7行返回错误,第8行不会?

  #include< iostream> 
using namespace std;

class Foo
{
public:
Foo()
{
cout< constructor Foo \\\
;
}
};

class Bar
{
public:
Bar(Foo)
{
cout< constructor Bar \\\
;
}
};

int main()
{
/ * 1 * / Foo * foo1 = new Foo();
/ * 2 * / Foo * foo2 = new Foo;
/ * 3 * / Foo foo3;
/ * 4 * / Foo foo4 = Foo :: Foo();

/ * 5 * / Bar * bar1 = new Bar(* new Foo());
/ * 6 * / Bar * bar2 = new Bar(* new Foo);
/ * 7 * / Bar * bar3 = new Bar(Foo foo5);
/ * 8 * / Bar * bar3 = new Bar(Foo :: Foo());

return 1;
}


解决方案

  / * 1 * / Foo * foo1 = new Foo(); 

在动态中创建 Foo 类型的对象记忆。 foo1 指向它。通常,你不会使用C ++中的原始指针,而是一个智能指针。如果 Foo 是POD类型,则会执行值初始化(此处不适用)。

  / * 2 * / Foo * foo2 = new Foo; 

与之前相同,因为 Foo

  / * 3 * / Foo foo3; 

创建 Foo 对象 foo3 在自动存储中。

  / * 4 * / Foo foo4 = Foo Foo() 

使用复制初始化创建 Foo

  / * 5 * / Bar在自动存储中调用 foo4  * bar1 = new Bar(* new Foo()); 

使用 Bar 的转换构造函数在动态存储中的 Bar 类型的对象。 bar1 是指向它的指针。

  / * 6 * / * bar2 = new Bar(* new Foo); 

与以前相同。

  / * 7 * / Bar * bar3 = new Bar(Foo foo5); 

这只是无效的语法。您不能在此声明变量。

  / * 8 * / Bar * bar3 = new Bar(Foo :: Foo )); 

如果 bar3 未在7中声明。



5&



语法如 new Bar(Foo :: Foo()); 不常见。通常 new Bar((Foo())); - 最讨厌的解析的额外括号帐户。 b $ b

What's the difference between lines 1 , 2 , 3 , 4?

When do I use each?

Why line 3 prints the constructor Foo and line 7 returns an error and line 8 doesn't?

#include <iostream>     
using namespace std;

class Foo
 {
   public:
   Foo ( )
   {
      cout << "constructor Foo\n";
   }               
};

class Bar
 {
   public:
   Bar ( Foo )
   {
      cout << "constructor Bar\n";
   }
};

int main()
{
   /* 1 */ Foo* foo1 = new Foo ();
   /* 2 */ Foo* foo2 = new Foo;
   /* 3 */ Foo foo3;
   /* 4 */ Foo foo4 = Foo::Foo();

   /* 5 */ Bar* bar1 = new Bar ( *new Foo() );
   /* 6 */ Bar* bar2 = new Bar ( *new Foo );
   /* 7 */ Bar* bar3 = new Bar ( Foo foo5 );
   /* 8 */ Bar* bar3 = new Bar ( Foo::Foo() );

   return 1;
}

解决方案

   /* 1 */ Foo* foo1 = new Foo ();

Creates an object of type Foo in dynamic memory. foo1 points to it. Normally, you wouldn't use raw pointers in C++, but rather a smart pointer. If Foo was a POD-type, this would perform value-initialization (it doesn't apply here).

   /* 2 */ Foo* foo2 = new Foo;

Identical to before, because Foo is not a POD type.

   /* 3 */ Foo foo3;

Creates a Foo object called foo3 in automatic storage.

   /* 4 */ Foo foo4 = Foo::Foo();

Uses copy-initialization to create a Foo object called foo4 in automatic storage.

   /* 5 */ Bar* bar1 = new Bar ( *new Foo() );

Uses Bar's conversion constructor to create an object of type Bar in dynamic storage. bar1 is a pointer to it.

   /* 6 */ Bar* bar2 = new Bar ( *new Foo );

Same as before.

   /* 7 */ Bar* bar3 = new Bar ( Foo foo5 );

This is just invalid syntax. You can't declare a variable there.

   /* 8 */ Bar* bar3 = new Bar ( Foo::Foo() );

Would work and work by the same principle to 5 and 6 if bar3 wasn't declared on in 7.

5 & 6 contain memory leaks.

Syntax like new Bar ( Foo::Foo() ); is not usual. It's usually new Bar ( (Foo()) ); - extra parenthesis account for most-vexing parse. (corrected)

这篇关于创建类的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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