实现一个对象 [英] Instansiating an object

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

问题描述

您好!


假设我有一个名为Student的班级,我就这样设置了一个名为kasia

的对象。

学生kasia(100);


如果我以这种方式实现这个kasia会有什么不同。

学生kasia =学生(100);


我认为这两个是等价的。但我总是使用以前的方式。


// Tony

解决方案

in Student kasia(100);一个带有一个参数的构造函数(可能是int)

将被调用。
学生kasia中的
=学生(100);调用复制构造函数。如果

你没有定义自己的,那么编译器默认生成它

,它将复制目标中所有数据成员的值。

通常应该选择以前的方式。


- 问候

Shivank



Tony Johansson写道:

你好!

假设我有一个名为Student的班级,我实现一个对象
以这种方式称为kasia。
学生kasia(100);


只有当你有一个参数化的构造函数时,这才有效。

接受一个整数作为参数。 Soo,在这种情况下,类decl。

应该有:


学生(int);


as其中一个构造函数,考虑到你没有任何默认的

参数构造函数。

如果我用这种方式实现这个kasia会有什么不同。
学生kasia =学生(100);

我认为这两个是等价的。但我总是使用以前的方式。


这两个不一样。实际上这不会调用副本

构造函数,因为您需要传递现有对象的副本。如果

,那么说:

学生kasia(100); //调用单参数构造函数

学生托尼(kaisa); //调用复制构造函数


然后实例化Tony会调用一个复制构造函数。


然而,在你的情况下如果调用必须通过它将需要

类似于:

学生(学生);


在你的班级中。但是如果你这样做,编译器(gcc 3.2.2)会抱怨说你可能想要一个复制构造函数,所以你需要

应该有:


学生(const学生&)

作为decl ..


所以,底线是你的第一次调用很好,它会调用一个

1参数构造函数来实例化你的Student对象。你的第二次

调用是不正确的,也不会调用复制构造函数。


希望这有点意义..
// Tony




Tony Johansson写道:

你好!

假设我有一个名为Student的类,我以这种方式实现了一个名为
kasia的对象。


你的意思是你实例化类,而不是对象。创建一个

的实例类(即一个对象)意味着实例化该类。

学生kasia(100);

有什么区别如果我以这种方式实现这个kasia。
学生kasia =学生(100);




第一个将创建kasia作为学生的实例和初始化

100。

第二个创建一个无名的临时Student实例,并且

用100初始化它。然后,kasia初始化从那个临时的

复制构造函数。然后临时被摧毁。许多现代的

编译器会优化临时性,而C ++标准明确地允许这样做,但即使这样,也必须能够访问复制构造函数。


Hello!

Assume I have a class named Student and I instansiate an object called kasia
in this way.
Student kasia(100);

What the difference if I instead instansiate this kasia in this way.
Student kasia = Student(100);

I assume these two are equivalent. But I always use former way.

//Tony

解决方案

in Student kasia(100); a constructor with one argument (may be int)
will be called.
in Student kasia = Student(100); the copy constructor is invoked. If
you have not defined your own, then compiler generates it by default
which will make a copy of values of all data members in the target.
One should normally prefer the former way.

-- Regards
Shivank



Tony Johansson wrote:

Hello!

Assume I have a class named Student and I instansiate an object called kasia in this way.
Student kasia(100);
This will work only if you have a parameterised constructor which
accepts an integer as a parameter. Soo, in this case the class decl.
should have :

Student(int);

as one of the constructors, considering you do not have any default
argument constructor.

What the difference if I instead instansiate this kasia in this way.
Student kasia = Student(100);

I assume these two are equivalent. But I always use former way.
No these two are not same. Actually this will not call the copy
constructor because you need to pass a copy of an existing object. If
it were, say:
Student kasia(100); // calls 1-argument constructor
Student Tony(kaisa); // calls the copy constructor

then instantiating Tony would have called a copy constructor.

However, In you case if the call has to go through it would require
something like:
Student(Student );

in your class decl. But if you do that, the compiler (gcc 3.2.2)would
complain saying that you probably wanted a copy constructor, so you
should have had :

Student(const Student &)

as the decl..

So, bottom line is your first call is fine and it makes a call to a
1-argument constructor to instantiate your Student object. Your second
call is not correct and it would NOT call a copy constructor.

Hope that makes some sense..
//Tony




Tony Johansson wrote:

Hello!

Assume I have a class named Student and I instansiate an object called
kasia in this way.
You mean you instantiate the class, not the object. Creating an instance of
the class (i.e. an object) means to instantiate that class.
Student kasia(100);

What the difference if I instead instansiate this kasia in this way.
Student kasia = Student(100);



The first one will create kasia as an instance of Student and initialize it
with 100.
The second one first creates a nameless temporary Student instance and
initializes it with 100. Then, kasia is initialized from that temporary by
the copy constructor. Then the temporary is destroyed. Many modern
compilers will optimize the temporary away, and the C++ standard explicitly
allows that, but even then, a copy constructor must be accessible.


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

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