VS2005 - 关于copy-ctor的问题 [英] VS2005 - question about copy-ctor

查看:85
本文介绍了VS2005 - 关于copy-ctor的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


在下面找到一个IMO应该拒绝编译的例子。用VS2005编译好的



通话中会发生什么?

你有什么看法?
< br $>
亲切的问候,

Patrick


类不可复制

{

受保护:

noncopyable(){}

~noncopyable(){}

private:

// hide副本和作业

noncopyable(noncopyable const&);

const noncopyable& operator =(noncopyable const&);

};

class A:public noncopyable //继承以避免复制

{

noncopyable copy_protection; //使成员避免复制

};

int main()

{

// A a ;

// A b = a; //这无法编译


A c = A(); //这个编译好了


返回0;

}

Hello all,

find below an example which IMO should refuse to compile. It compiles fine
with VS2005.

What happens in the call?
What is your opinion?

Kind regards,
Patrick

class noncopyable
{
protected:
noncopyable() {}
~noncopyable() {}
private:
// hide the copy-ctor and assignment
noncopyable( noncopyable const & );
const noncopyable& operator=( noncopyable const & );
};
class A : public noncopyable // inherit to avoid copying
{
noncopyable copy_protection; // make member to avoid copying
};
int main()
{
//A a;
//A b = a; // this fails to compile

A c = A(); // this compiles fine

return 0;
}

推荐答案

Patrick Kowalzick写道:
Patrick Kowalzick wrote:
大家好,

在下面找到一个IMO应该拒绝编译的例子。用VS2005编译好。

通话中会发生什么?
你有什么看法?


我同意它不应该编译,并且有两个不同的原因:


1:'的默认构造函数无法访问

成员copy_protection的默认构造函数 - 请记住,受保护的访问只能通过指针,引用和

给你

访问受保护的基础成员派生类的对象,而不是基类(这是为了防止一个

派生类破坏来自同一基础的其他派生类的对象派生

)。结果,A没有格式正确的默认构造函数,所以即使是A a;也是如此。不应该编译。这包含在标准的11.5 / 1

中。


删除会员copy_protection来解决这个问题,并尝试一下

不同的编译器首先得到错误。


2:在通话中,c是从默认构造的A初始化的复制。

允许编译器优化副本,但根据

标准,它必须检查是否可以制作副本(例如,

是一个可访问的拷贝构造函数)。这在标准的12.2 / 1中有所涉及。


再次,使用不同的编译器来获取错误。

A类:公共不可复制//继承避免复制
{noncopyable copy_protection; //使成员避免复制
};


应该是:


A类:不可复制//不需要公共继承

{

};

int main()
//
// A b = a; //这无法编译

A c = A(); //这个编译好了

返回0;
}
Hello all,

find below an example which IMO should refuse to compile. It compiles fine
with VS2005.

What happens in the call?
What is your opinion?
I agree that it shouldn''t compile, and for 2 separate reasons:

1: A''s default constructor cannot access the default constructor for the
member copy_protection - remember that protected access only gives you
access to protected members of bases through pointers, references and
objects of the derived class, not the base class (this is to prevent one
derived class from breaking objects of other derived classes derived
from the same base). As a result, A doesn''t have a well-formed default
constructor, so even "A a;" shouldn''t compile. This is covered in 11.5/1
of the standard.

Delete the member copy_protection to get around this, and try a
different compiler to get the error in the first place.

2: In the call, "c" is copy initialized from a default constructed A.
The compiler is allowed to optimize out the copy, but according to the
standard it must check that the copy could be made (e.g. that there is
an accessible copy constructor). This is covered in 12.2/1 of the standard.

Again, use a different compiler to get the error.
class A : public noncopyable // inherit to avoid copying
{
noncopyable copy_protection; // make member to avoid copying
};
Should be:

class A : noncopyable // no need for public inheritance
{
};


int main()
{
//A a;
//A b = a; // this fails to compile

A c = A(); // this compiles fine

return 0;
}




您可能想要向MS报告错误。


Tom



You may want to report the bugs to MS.

Tom


Hello Tom,
Hello Tom,

我同意它不应该'编译,并且有两个不同的原因:

1:'的默认构造函数无法访问
成员copy_protection的默认构造函数 - 请记住,受保护的访问只能为您提供
通过指针,引用和派生类的对象访问受保护的基础成员,而不是基类(这是为了防止一个派生类破坏从
相同的基地)。结果,A没有格式良好的默认构造函数,所以即使是A a;也是如此。不应该编译。这将在标准的11.5 / 1中进行介绍。

删除成员copy_protection以解决此问题,并尝试使用不同的编译器来获取错误。


通常,我不使用成员copy_protection。这只是为了测试VS2005

:)。

2:在通话中,c是从默认构造的A初始化的复制。
编译器可以优化复制,但根据
标准,它必须检查是否可以复制(例如,有一个可访问的复制构造函数)。这在标准的12.2 / 1中有所涉及。

同样,使用不同的编译器来获取错误。


VS2005是第一个,我没有得到错误。我有点感到惊讶,因为这是非常基本的东西。但也许MS将很快推出8.1

:)。 8.0有时候有点烦人。

I agree that it shouldn''t compile, and for 2 separate reasons:

1: A''s default constructor cannot access the default constructor for the
member copy_protection - remember that protected access only gives you
access to protected members of bases through pointers, references and
objects of the derived class, not the base class (this is to prevent one
derived class from breaking objects of other derived classes derived from
the same base). As a result, A doesn''t have a well-formed default
constructor, so even "A a;" shouldn''t compile. This is covered in 11.5/1
of the standard.

Delete the member copy_protection to get around this, and try a different
compiler to get the error in the first place.
Normally, I do not use a member copy_protection. It was just to test VS2005
:).
2: In the call, "c" is copy initialized from a default constructed A. The
compiler is allowed to optimize out the copy, but according to the
standard it must check that the copy could be made (e.g. that there is an
accessible copy constructor). This is covered in 12.2/1 of the standard.

Again, use a different compiler to get the error.
VS2005 is the first, where I do not get the error. I was a little bit
surprised, as this is quite basic stuff. But perhaps MS will launch a 8.1
soon :). 8.0 is a little bit annoying sometimes.
A类:公共不可复制//继承以避免复制
{non / textable copy_protection ; //使成员避免复制
};
class A : public noncopyable // inherit to avoid copying
{
noncopyable copy_protection; // make member to avoid copying
};



应该是:

A类:不可复制//不需要公共继承 {
};



Should be:

class A : noncopyable // no need for public inheritance
{
};




True。

您可能想要向MS报告错误。



True.
You may want to report the bugs to MS.




是的,我想要。有我要报告的antoherone,但我不知道怎么做。我用了一段时间用谷歌搜索了一下,但还没有成功。


问候,

Patrick



Yes, I want. There is antoherone I want to report, but I do not know how. I
googled a little bit, but was not successful, yet.

Regards,
Patrick


您可能想要向MS报告错误。
You may want to report the bugs to MS.



是的,我想。有我要报告的antoherone,但我不知道怎么做。
我用谷歌搜索了一下,但还没有成功。



Yes, I want. There is antoherone I want to report, but I do not know how.
I googled a little bit, but was not successful, yet.




你可以在此处执行此操作:
http://lab.msdn。 microsoft.com/produc...k/default.aspx


然后,在你这样做之后,你可以在这里发布网址,以便我们可以验证

并投票给它。


-


亲切的问候,

Bruno van Dooren
br ********* *************@hotmail.com

仅删除_nos_pam



You can do that here:
http://lab.msdn.microsoft.com/produc...k/default.aspx

Then, after you did that you can post the url here so that we can validate
it and vote for it.

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"


这篇关于VS2005 - 关于copy-ctor的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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