编译器生成还是定制

编译器生成还是定制

本文介绍了C ++赋值运算符 - 编译器生成还是定制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个中等复杂的C ++类,它保存从光盘读取的一组数据。它包含浮游物,ints和结构的混合混合,现在一般使用。在一个主要的代码审查期间,被问及是否有一个自定义赋值运算符,或者我们依赖于编译器生成的版本,如果是,我们怎么知道它正确工作?好了,我们没有写自定义赋值,所以添加了一个单元测试来检查,如果我们这样做:

I have a medium complex C++ class which holds a set of data read from disc. It contains an eclectic mix of floats, ints and structures and is now in general use. During a major code review it was asked whether we have a custom assignment operator or we rely on the compiler generated version and if so, how do we know it works correctly? Well, we didn't write a custom assignment and so a unit test was added to check that if we do:

CalibDataSet datasetA = getDataSet();
CalibDataSet dataSetB = datasetA;

那么datasetB与datasetA相同。几百线左右。现在客户认为我们不能依赖于编译器(gcc)是正确的未来版本,我们应该写我们自己的。他们是否有权坚持这样做?

then datasetB is the same as datasetA. A couple of hundred lines or so. Now the customer inists that we cannot rely on the compiler (gcc) being correct for future releases and we should write our own. Are they right to insist on this?

其他信息:

已经发布和响应时间。另一种提出这个问题的方法可能是:
POD结构/类何时变成非POD结构/类?

I'm impressed by the answers/comments already posted and the response time.Another way of asking this question might be:When does a POD structure/class become a 'not' POD structure/class?

推荐答案

众所周知,自动生成的赋值操作符会做什么 - 它被定义为标准的一部分,并且符合标准的C ++编译器将始终生成一个正确的赋值操作符(如果没有,那么它不会是一个符合标准的编译器)。

It is well-known what the automatically-generated assignment operator will do - that's defined as part of the standard and a standards-compliant C++ compiler will always generate a correctly-behaving assignment operator (if it didn't, then it would not be a standards-compliant compiler).

你通常只需要编写自己的赋值运算符,自己的析构函数或复制构造函数。如果你不需要那些,那么你不需要一个赋值运算符。

You usually only need to write your own assignment operator if you've written your own destructor or copy constructor. If you don't need those, then you don't need an assignment operator, either.

这篇关于C ++赋值运算符 - 编译器生成还是定制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 00:55