避免自动复制构造函数生成 [英] Avoid automatic copy constructor generation

查看:56
本文介绍了避免自动复制构造函数生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




有办法避免自动复制构造函数生成。我不希望对象不可复制。
不希望该对象不可复制。我根本不希望

复制是由默认的复制构造函数完成的。但是有一个接受基类的

构造函数。这个应该用于

复制。


实际上我有一组具有共同抽象基础的类。

实现没有自己的数据成员。他们只实现了不同的功能。但在这些类型之间进行任务并复制

构造是有意义的。这有效地改变了

功能,具体取决于当前类型。

class ContainerBase

{// ...

};


class Container1

:public ContainerBase

{//没有自己的数据成员!

public:

Container1()

{}

Container1(const ContainerBase& r)

:ContainerBase (r)

{}

Container1& operator =(const ContainerBase& r)

{ContainerBase :: operator =(r);返回*这个; }

};


类Container2

:public ContainerBase

{//没有自己的数据会员!

公开:

Container2()

{}

Container2(const ContainerBase& r)

:ContainerBase(r)

{}

Container2& operator =(const ContainerBase& r)

{ContainerBase :: operator =(r);返回*这个; }

};

int main()

{Container1 c1;

Container2 c2 = c1; //好的

Container1 c3 = c1; //错!调用Container1(const Contarine1& r)

返回0;

}

当然,我可以实现复制构造函数来做同样的事情

作为const ContainerBase&的构造函数。但这确实是不需要的冗余。复制构造函数在实际应用程序中比在这个小例子中要复杂得多。此外,auto

生成的复制构造函数无法使用。由于一些特殊的内部存储依赖性,它具有未定义的行为

。它还必须具有多重继承的
,所以我无法将代码移动到成员

函数。


有没有办法避免每个类需要重新定义副本

构造函数?

Marcel

解决方案

MarcelMüller写道:


有一种方法可以避免自动复制构造函数的生成。



是:实现自己的复制构造函数。这将使编译器

不创建默认值。


MarcelMüller写道:





有一种方法可以避免自动复制构造函数的生成。我不希望对象不可复制。
不希望该对象不可复制。我根本不希望

复制是由默认的复制构造函数完成的。但是有一个接受基类的

构造函数。这个应该用于

复制。


实际上我有一组具有共同抽象基础的类。

实现没有自己的数据成员。他们只实现了不同的功能。但在这些类型之间进行任务并复制

构造是有意义的。这有效地改变了

功能,具体取决于当前类型。



[snip]


如何为Base实现一个拷贝构造函数然后再使用


class Derived_1:public Base {


Derived_1(Base const& other)

:Base(other)

{}


Derived_1(Derived_1 const& other)

:基础(其他)

{}


};


和Derived_2类似,......?


我想,如果Base的copy-constructor是正确实现的,派生类的

编译器生成的拷贝构造函数可以和上面一样做

,所以你甚至可以沟通那。现在,_that_看起来非常像代码,我剪断了。我显然不明白你的

问题。也许,你需要发布更多细节,因为我缺少

想象力:


此外自动

生成的复制构造函数不可用。由于一些特殊的内部存储依赖性,它具有未定义的行为

。它还必须具有多重继承的
,所以我无法将代码移动到成员

函数。



如果这是真的,我看不出构造函数_from_ base_

如果它们只是正常工作:


Container1(const ContainerBase& r)

:ContainerBase(r)

{}




Best


Kai-Uwe Bux


Juha Nieminen写道:


MarcelMüller写道:


>有没有办法避免自动复制构造函数生成。



是:实现自己的复制构造函数。这将使编译器

不创建默认值。



你不必实现它。只要声明一个。如果你的程序确实没有使用它,你就不必定义它。并确保程序

不使用它,声明它是'私人'。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


Hi,

is there a way to avoid the automatic copy constructor generation. I do
not want the object to be non-copyable. I simply do not want that
copying is done by the default copy constructor. But there is a
constructor that accepts the base class. This one should be used for
copying.

In fact i have a set of classes with a common abstract base. The
implementations do not have own data members. They only implement
different functionality. But is makes sense to do assignments and copy
construction between these set of types. This effectively changes the
functionality depending on the current type.
class ContainerBase
{ // ...
};

class Container1
: public ContainerBase
{ // no own data members!
public:
Container1()
{}
Container1(const ContainerBase& r)
: ContainerBase(r)
{}
Container1& operator=(const ContainerBase& r)
{ ContainerBase::operator=(r); return *this; }
};

class Container2
: public ContainerBase
{ // no own data members!
public:
Container2()
{}
Container2(const ContainerBase& r)
: ContainerBase(r)
{}
Container2& operator=(const ContainerBase& r)
{ ContainerBase::operator=(r); return *this; }
};
int main()
{ Container1 c1;
Container2 c2 = c1; // OK
Container1 c3 = c1; // Wrong! Invokes Container1(const Contarine1& r)
return 0;
}
Of course, I could implement the copy constructors to do the same thing
as the constructor from const ContainerBase&. But this is really
unneeded redundancy. And the copy constructor is much more complicated
in the real application than in this small example. Furthermore the auto
generated copy constructor is unusable. It has undefined behavior
because of some special internal storage dependencies. It also has to do
with multiple inheritance, so I could not move the code to a member
function either.

Is there a way to avoid the required redefinition of the copy
constructor for each class?
Marcel

解决方案

Marcel Müller wrote:

is there a way to avoid the automatic copy constructor generation.

Yes: Implement your own copy constructor. That will make the compiler
to not to create a default one.


Marcel Müller wrote:

Hi,

is there a way to avoid the automatic copy constructor generation. I do
not want the object to be non-copyable. I simply do not want that
copying is done by the default copy constructor. But there is a
constructor that accepts the base class. This one should be used for
copying.

In fact i have a set of classes with a common abstract base. The
implementations do not have own data members. They only implement
different functionality. But is makes sense to do assignments and copy
construction between these set of types. This effectively changes the
functionality depending on the current type.

[snip]

What about implementing a copy-constructor for the Base and then have

class Derived_1 : public Base {

Derived_1 ( Base const & other )
: Base ( other )
{}

Derived_1 ( Derived_1 const & other )
: Base ( other )
{}

};

and similarly for Derived_2, ... ?

I think, if the copy-constructor for Base is implemented properly, the
compiler-generated copy-constructor for the derived classes would do the
same thing as the above, so you could even ditch that. Now, _that_ looks
very much like the code, I snipped. I clearly do not understand your
problem. Maybe, you need to post more details, because I lack the
imagination for:

Furthermore the auto
generated copy constructor is unusable. It has undefined behavior
because of some special internal storage dependencies. It also has to do
with multiple inheritance, so I could not move the code to a member
function either.

If that is true, I do not see how the constructors _from_ the base can
possibly work correctly if they are just:

Container1(const ContainerBase& r)
: ContainerBase(r)
{}



Best

Kai-Uwe Bux


Juha Nieminen wrote:

Marcel Müller wrote:

>is there a way to avoid the automatic copy constructor generation.


Yes: Implement your own copy constructor. That will make the compiler
to not to create a default one.

You don''t have to implement it. Just declare one. If your program does
not use it, you don''t have to define it. And to make sure the program
does not use it, declare it ''private''.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


这篇关于避免自动复制构造函数生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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