默认情况下,所有类都有 [英] what all a class has by default

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

问题描述

大家好,

i想知道我什么时候创建一个类。它包含的全部内容。我想知道如果我没有申报,默认情况下会有以下内容他们自己承诺
。请告诉剩下的其他事情。


1.构造函数

2.Destructor
3.Copy构造函数

4.分配运营商


提前致谢。

Hi all,
i want to know when i create a class.what all it contains.I
know the following things are there by default if i do not declare them
by myself.Please tell the other things that are left.

1. constructor
2.Destructor
3.Copy constructor
4. Assignment operator

Thanks in advance.

推荐答案



Ajay写道:

Ajay wrote:
大家好,
我想知道我何时创建一个类。所有它包含。我知道如果我不自己声明它们,默认情况下会有以下内容。请告诉剩下的其他内容。

1.构造函数


您需要更具体地了解这个。编译器将生成

a默认构造函数(即,没有参数调用的构造函数)

_only_如果你没有声明_any_构造函数。

2.Destructor
3.Copy构造函数
4.赋值运算符
Hi all,
i want to know when i create a class.what all it contains.I
know the following things are there by default if i do not declare them
by myself.Please tell the other things that are left.

1. constructor
You need to be more specific with this one. The compiler will generate
a default constructor (that is, a constructor called with no arguments)
_only_ if you do not declare _any_ constructors.
2.Destructor
3.Copy constructor
4. Assignment operator




正确。对于每一个,如果你还没有宣布编译器

将为你生成它。 三规则说如果你需要

定义你自己实现的那三个中的一个,因为

编译器生成了一个做错了,你可能需要

定义所有三个。


Gavin Deane



Correct. For each of those, if you haven''t declared it the compiler
will generate it for you. The "Rule of Three" says that if you need to
define your own implementation of one of those three because the
compiler generated one does the wrong thing, you probably need to
define all three.

Gavin Deane


如果你没有实现自己的析构函数,编译器可能会自动生成

或者不生成任何内容。但它没有生成一个

做错了。

If you do not implement your own Destructor, a compiler maybe generates
one automatically or generates nothing,. BUT it does not generate one
does the wrong thing .




?μ·é £?写道:

?μ·é£? wrote:
如果您没有实现自己的析构函数,编译器可能会自动生成一个或不生成任何内容。但它没有生成一个
做错了。
If you do not implement your own Destructor, a compiler maybe generates
one automatically or generates nothing,. BUT it does not generate one
does the wrong thing .




我认为这取决于错误的意思,所以让我<澄清。


符合标准的编译器当然会生成一个析构函数,它完全符合标准所说的编译生成的
破坏者。所以在

中,析构函数并没有做出错误。没事。


但是,给定这个课程


#include< string>


class foo

{

public:

foo():pstr(new std :: string(" Leak")){}

私人:

//我们只是禁用这些

foo(const foo&);

foo& operator =(const foo&);


std :: string * pstr;

};


编译器生成的析构函数不会删除构造函数中分配的字符串

,因此foo类型的每个对象都会在销毁时泄漏内存

。这(大概)不是你想要的行为

所以你需要自己编写析构函数。这就是我所说的

编译器生成的析构函数所做的错误操作。事情。它完全符合标准所说的应该做的事情,但我需要它来做某事

其他。


Gavin Deane



Well I suppose that depends on what one means by "wrong", so let me
clarify.

A conforming compiler will, of course, generate a destructor that does
exactly what the standard says compiler-generated destructors do. So in
that sense, the destructor is not doing the "wrong" thing.

However, given this class

#include <string>

class foo
{
public:
foo() : pstr(new std::string("Leak")) {}
private:
// Let''s just disable these
foo(const foo&);
foo& operator=(const foo&);

std::string* pstr;
};

the compiler-generated destructor will not delete the string allocated
in the constructor and so every object of foo type will leak memory
when it is destroyed. This is (presumably) not the behaviour you want
so you need to write the destructor yourself. That''s what I mean by the
compiler-generated destructor doing the "wrong" thing. It does exactly
what the standard says it should do, but I need it to do something
else.

Gavin Deane


这篇关于默认情况下,所有类都有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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