如果构造函数可用于初始化... [英] if Constructor can be used to initialize ...

查看:85
本文介绍了如果构造函数可用于初始化...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




如果可以使用构造函数进行初始化,何时分配内存

/说新运营商等?


谢谢



If a Constructor can be used to initialize, when is memory is allocated
/ say the "new" operator etc?

Thanks

推荐答案

* 2005:
* 2005:

>

如果可以使用构造函数进行初始化,则何时分配内存

/表示new运营商等?
>
If a Constructor can be used to initialize, when is memory is allocated
/ say the "new" operator etc?



在调用构造函数之前的某个时间。


对于new以前的操作员。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?

Sometime before the constructor is called.

For the "new" operator that''s immediately before.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


这是否意味着如果我们使用构造函数,我们不需要一个new

运算符?

谢谢


Alf P. Steinbach写道:
Does it mean that if we use a constructor, we don''t need a "new"
operator?

Thanks

Alf P. Steinbach wrote:

* 2005:
* 2005:


如果可以使用构造函数进行初始化,则何时分配内存

/表示new运营商等?

If a Constructor can be used to initialize, when is memory is allocated
/ say the "new" operator etc?



在调用构造函数之前的某个时间。


对于new以前的操作员。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?


Sometime before the constructor is called.

For the "new" operator that''s immediately before.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?




2005写道:

2005 wrote:

如果构造函数可以用于初始化,何时分配内存

/说新运营商等?


谢谢
If a Constructor can be used to initialize, when is memory is allocated
/ say the "new" operator etc?

Thanks



你不需要新的运营商来分配 - 在一个完美的世界里,使用

new应该是不鼓励的。

ctor'的主要目的是根据其类型的

内容分配一个实例(忽略现在继承的类)和任何

资源是必需的。

在C ++中,它恰好具有初始化

成员的次要目的,如果有的话,使用init列表。初始化不是一个

的要求,但经常被忽视并且非常有用。


回答你的问题:当构造函数是$ / $时分配发生b $ b被调用。

和invoke并不意味着打电话。


#include< iostream>


class N

{< br $>
int n;

public:

N():n(0){std :: cout<< invoke ctor\\\
; }

~N(){std :: cout<< invoke d~tor\ n; }

const int& get()const {return n; }

};


int main()

{

N实例; //分配发生在这里

std :: cout<< instance.n = << instance.get()<< std :: endl;


返回0;

} //释放发生在这里 - 在范围结束时


/ *

调用ctor

instance.n = 0

调用d / tor

* /


解释在常见问题解答中:
http://www.parashift.com/c++-faq-lite/ctors.html

You don''t need the new operator to allocate - in a perfect world, using
new should be discouraged.
A ctor''s primary purpose is to allocate an instance based on the
contents of its type (ignoring inherited classes for now) and whatever
resources are required.
In C++ it happens to have a secondary purpose that is to initialize
members, if any, using an init list. The initialisation is not a
requirement but too often overlooked and quite usefull.

To answer your question: allocation happens when the constructor is
invoked.
and "invoke" does not mean "call".

#include <iostream>

class N
{
int n;
public:
N() : n(0) { std::cout << "invoke ctor\n"; }
~N() { std::cout << "invoke d~tor\n"; }
const int& get() const { return n; }
};

int main()
{
N instance; // allocation happens here
std::cout << "instance.n = " << instance.get() << std::endl;

return 0;
} // deallocation happens here - at end of scope

/*
invoke ctor
instance.n = 0
invoke d~tor
*/

The explanation is in the FAQ:
http://www.parashift.com/c++-faq-lite/ctors.html


这篇关于如果构造函数可用于初始化...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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