编译器为结构生成的默认ctor等? [英] Default ctor, etc generated by compiler for structs?

查看:67
本文介绍了编译器为结构生成的默认ctor等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是一个默认的构造函数,析构函数,复制构造函数和赋值

运算符是否由编译器为结构生成,如果它们没有明确定义

定义了什么?


我认为答案是肯定的,因为除了公共/私人访问规范之外,结构

和类之间没有区别。 (以及一些小事

其他事情)。当我创建一个类时,我总是首先声明

默认构造函数,复制构造函数和赋值运算符私有与

没有实现。我不会为结构做那些(我认为结构类似于他们在C中的b $ b,但它们实际上并没有实现到

我知道) 。我在想我不会处理相同的结构因为我相信

编译器在结构的情况下做正确的事情然后按位

复制为例如,课程可能不是所期望的。


以上推理是否可以?我应该继续不继续声明/定义

默认的结构吗?


John

解决方案



JohnQ< jo *********************** @ yahoo.comwrote in message ...


是一个默认的构造函数,析构函数,复制构造函数和赋值

运算符由编译器生成的结构如果它们不是



explicitely


定义了什么?



#include< vector>

struct Hmmm {int mmm; };


{

std :: vector< HmmmSVmmm(2);

std :: vector< HmmmSVtwo;

SVtwo = SVmmm;

}


可以吗?它是否符合std容器的要求?

[我的测试说:是的,没问题,伙计! ]

现在,在结构中添加一些非简单的东西,然后重新测试。


>

我认为答案是肯定的,因为



struct

$之间没有区别b $ b和除公共/私人访问规范之外的类别 (以及一些



minor


其他内容)。当我创建一个类时,我总是首先声明

默认构造函数,复制构造函数和赋值运算符私有与

没有实现。我不会为结构做那些(我认为结构

就像它们在C中一样,但它们实际上并没有实现到目前为止



as


我知道)。我在想我不会处理相同的结构因为我相信

编译器在结构的情况下做正确的事情然后按位

复制为例如,课程可能不是所期望的。


以上推理是否可以?我应该继续不继续声明/定义结构的

默认内容吗?

John



class A {public :int a;}; == struct A {int a;};

class A {int a;}; == struct A {private:int a;};


或者我误解了什么(再次!)。


-

Bob R

POVrookie


JohnQ写道:


是一个默认的构造函数,析构函数,复制构造函数和赋值如果它们不是明确定义的
,编译器为结构生成的
运算符?

我认为答案是肯定的,因为除了公共/私人访问规范之外,

结构和类之间没有区别。 (和

一些其他的小事)。当我创建一个类时,我总是从

开始声明默认构造函数,复制构造函数和赋值

运算符私有而没有实现。我不会那样做结构

虽然(我认为结构就像它们在C中一样,但据我所知它们实际上并不是

) 。我在想我不会因为我相信编译器在结构化的情况下做了正确的事情而不是
处理结构,而是按位进行复制例如,一个班级可能不是什么需要的。


以上推理可以吗?我应该继续不继续声明/定义结构的

默认内容吗?



应用与您的类相同的规则。如果您需要在创建或复制时初始化成员,则需要构造函数。


-

Ian Collins。


" JohnQ" < jo *********************** @ yahoo.com写了留言

新闻:Hy ******* ********* *@newssvr19.news.prodigy.ne t ...


是默认构造函数,析构函数,复制构造函数和赋值

运算符由编译器为结构生成,如果它们不是明确定义的



我认为答案是肯定的,因为除了公共/私人访问规范之外,

结构和类之间没有区别 (以及

其他一些小事)。当我创建一个类时,我总是从

开始声明默认构造函数,复制构造函数和赋值

运算符私有而没有实现。我不是那样做结构

虽然(我认为结构就像他们在C中,但他们真的不在

实现据我所知) 。我在想我不会处理

结构相同,因为我相信编译器在

结构中执行正确的操作,而按位复制为例如,一个类可能不是什么是




以上推理是否可以?我应该继续不继续声明/定义结构的

默认内容吗?



如果您的结构是POD,那么应该没有理由不允许

编译器创建默认构造函数,析构函数,复制构造函数和

赋值运算符。我会说这没关系,但是我不知道

究竟是什么原因你为了克服4级课程(不是说它很糟糕

的东西,可能是非常好的,我只是不知道你的理由

it。


Are a default constructor, destructor, copy constructor and assignment
operator generated by the compiler for a struct if they are not explicitely
defined?

I think the answer is yes, because "there is no difference between a struct
and a class except the public/private access specification" (and a few minor
other things). When I create a class, I always start by declaring the
default constructor, copy constructor and assignment operator private with
no implementation. I don''t do that for structs though (I consider structs
like they were in C, but they really are not in the implementation as far as
I know). I''m thinking that I don''t handle structs the same because I trust
the compiler to do the right thing in the case of structs whereas bitwise
copy for a class may not be what is desired, for example.

Is the above reasoning OK? Should I continue NOT declaring/defining the
default stuff for structs?

John

解决方案


JohnQ <jo***********************@yahoo.comwrote in message...

Are a default constructor, destructor, copy constructor and assignment
operator generated by the compiler for a struct if they are not

explicitely

defined?

#include <vector>
struct Hmmm{ int mmm; };

{
std::vector<HmmmSVmmm(2);
std::vector<HmmmSVtwo;
SVtwo = SVmmm;
}

Can that work? Does it meet the requirements for a std container?
[ my tests say: yes, no problem, dude! ]
Now, add something non-simple to the struct, and test again.

>
I think the answer is yes, because "there is no difference between a

struct

and a class except the public/private access specification" (and a few

minor

other things). When I create a class, I always start by declaring the
default constructor, copy constructor and assignment operator private with
no implementation. I don''t do that for structs though (I consider structs
like they were in C, but they really are not in the implementation as far

as

I know). I''m thinking that I don''t handle structs the same because I trust
the compiler to do the right thing in the case of structs whereas bitwise
copy for a class may not be what is desired, for example.

Is the above reasoning OK? Should I continue NOT declaring/defining the
default stuff for structs?
John

class A{ public: int a;}; == struct A{ int a;};
class A{ int a;}; == struct A{ private: int a;};

Or did I misunderstand something (again!).

--
Bob R
POVrookie


JohnQ wrote:

Are a default constructor, destructor, copy constructor and assignment
operator generated by the compiler for a struct if they are not
explicitely defined?

I think the answer is yes, because "there is no difference between a
struct and a class except the public/private access specification" (and
a few minor other things). When I create a class, I always start by
declaring the default constructor, copy constructor and assignment
operator private with no implementation. I don''t do that for structs
though (I consider structs like they were in C, but they really are not
in the implementation as far as I know). I''m thinking that I don''t
handle structs the same because I trust the compiler to do the right
thing in the case of structs whereas bitwise copy for a class may not be
what is desired, for example.

Is the above reasoning OK? Should I continue NOT declaring/defining the
default stuff for structs?

Apply the same rules as you apply to your classes. If you need to
initialise members on creation or copy, you need constructors.

--
Ian Collins.


"JohnQ" <jo***********************@yahoo.comwrote in message
news:Hy*****************@newssvr19.news.prodigy.ne t...

Are a default constructor, destructor, copy constructor and assignment
operator generated by the compiler for a struct if they are not
explicitely defined?

I think the answer is yes, because "there is no difference between a
struct and a class except the public/private access specification" (and a
few minor other things). When I create a class, I always start by
declaring the default constructor, copy constructor and assignment
operator private with no implementation. I don''t do that for structs
though (I consider structs like they were in C, but they really are not in
the implementation as far as I know). I''m thinking that I don''t handle
structs the same because I trust the compiler to do the right thing in the
case of structs whereas bitwise copy for a class may not be what is
desired, for example.

Is the above reasoning OK? Should I continue NOT declaring/defining the
default stuff for structs?

If your structure is POD, then there should be no reason not to allow the
compiler to create default constructor, destrucctor, copy constructor and
assignment operators. I would say it was okay, but I do not know for
exactly what reason you crete the 4 for classes (not saying it''s a bad
thing, probably a very good thing, I just don''t know your reasoning behind
it).


这篇关于编译器为结构生成的默认ctor等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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