使用无法访问的基类复制c-tor复制构造 [英] Copy construction with inaccessible base class copy c-tor
问题描述
你好,
看一下这个程序:
------------------ -----------------
B级
{
B(const B&) ;
B& operator =(const B&);
public:
B(int);
};
class C:public B
{
public:
C(int);
};
int main()
{
C c = C(42); // Comeau:没有错误
B b = B(42); //:Comeau:错误
}
------------------------ -----------
根据我的理解,c和b都是复制构造的。
B复制c-tor是无法访问的,所以''b''的构造是错误的。并且Comeau(在线试驾)标志着它。
但它允许构建''c''。应该吗?
据我所知,暂时可以省略,但它的创建应该是可能的(12.2 / 1),好像它不是'省略了。
由于C'的复制c-tor无法创建(12.8 / 7),因此需要(或将需要)的代码
也是不正确的。
我在哪里犯错?或者我呢?
谢谢!
V
-
请在通过电子邮件回复时删除资本''A'
我没有回复最热门的回复,请不要问
[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]
[comp.lang.c ++。moderated。第一次海报:做到这一点! ]
Hello,
Take a look at this program:
-----------------------------------
class B
{
B(const B&);
B& operator=(const B&);
public:
B(int);
};
class C : public B
{
public:
C(int);
};
int main()
{
C c = C(42); // Comeau: no error
B b = B(42); // Comeau: error
}
-----------------------------------
Both ''c'' and ''b'' are copy-constructed, as I understand it.
B''s copy c-tor is inaccessible, so construction of ''b'' is
ill-formed. And Comeau (online test drive) flags it such.
But it lets the construction of ''c'' through. Should it?
As I understand it, a temporary can be omitted, but its
creation should be possible (12.2/1) as if it weren''t omitted.
And since C''s copy c-tor cannot be created (12.8/7), the code
that requires (or would require) it is also ill-formed.
Where do I err? Or do I?
Thanks!
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
推荐答案
你好,
2月26日,9:35 pm," ; Victor Bazarov < v.Abaza ... @ comAcast.netwrote:
Hello,
On Feb 26, 9:35 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
你好,
看看这个程序:
-----------------------------------
级B
{
B(const B&);
B& operator =(const B&);
public:
B(int);
};
C级:公共B
{
公开:
C(int);
};
int main()
{
C c = C(42); // Comeau:没有错误
B b = B(42); // Comeau:错误
}
-------------------- ---------------
正如我所理解的,c和b都是复制构造的。
Hello,
Take a look at this program:
-----------------------------------
class B
{
B(const B&);
B& operator=(const B&);
public:
B(int);
};
class C : public B
{
public:
C(int);
};
int main()
{
C c = C(42); // Comeau: no error
B b = B(42); // Comeau: error
}
-----------------------------------
Both ''c'' and ''b'' are copy-constructed, as I understand it.
如果一切正常,将调用复制构造函数。
If everything were right the copy constructor would be called.
B'的副本c- tor是无法访问的,所以''b''的构造是错误的。而且Comeau(在线试驾)标志着它。
B''s copy c-tor is inaccessible, so construction of ''b'' is
ill-formed. And Comeau (online test drive) flags it such.
是的。 b不能复制构造。
yes. b cannot be copy constructed.
但是它允许构造''c''通过。应该吗?
But it lets the construction of ''c'' through. Should it?
NO。 c也不能复制。编译器不会生成
a复制构造函数,因为基类复制构造函数是私有的。
所以这也是一个错误。
NO. c cannot be copy constructed as well. Compiler shall not generate
a copy constructor as the base class copy constructor is private.
SO this is an error too.
据我所知,临时可以省略,但它的创建应该是可能的(12.2 / 1),好像它没有被省略。
由于无法创建C'的复制c-tor(12.8 / 7),因此需要(或将要求)的代码
也是格式错误。
我在哪里犯错?或者我呢?
谢谢!
V
-
请在通过电子邮件回复时删除资本''A'
我没有回复最热门的回复,请不要问
As I understand it, a temporary can be omitted, but its
creation should be possible (12.2/1) as if it weren''t omitted.
And since C''s copy c-tor cannot be created (12.8/7), the code
that requires (or would require) it is also ill-formed.
Where do I err? Or do I?
Thanks!
V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask
-
[见 http: //www.gotw.ca/resources/clcm.htm 了解有关的信息]
[comp.lang.c ++。moderated。第一次海报:做到这一点! ]
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Victor Bazarov写道:
Victor Bazarov wrote:
您好,
看一下这个程序:
------------------------------- ----
B级
{
B(const B&);
B& operator =(const B&);
public:
B(int);
};
class C:public B
{
public:
C(int);
};
int main()
{
C c = C(42); // Comeau:没有错误
B b = B(42); //:Comeau:错误
}
------------------------ -----------
根据我的理解,c和b都是复制构造的。
B复制c-tor是无法访问的,所以''b''的构造是错误的。并且Comeau(在线试驾)标志着它。
但它允许构建''c''。应该吗?
Hello,
Take a look at this program:
-----------------------------------
class B
{
B(const B&);
B& operator=(const B&);
public:
B(int);
};
class C : public B
{
public:
C(int);
};
int main()
{
C c = C(42); // Comeau: no error
B b = B(42); // Comeau: error
}
-----------------------------------
Both ''c'' and ''b'' are copy-constructed, as I understand it.
B''s copy c-tor is inaccessible, so construction of ''b'' is
ill-formed. And Comeau (online test drive) flags it such.
But it lets the construction of ''c'' through. Should it?
听起来很合理。 gcc拒绝这两行,Sun CC只有C c = C(42)的barf;
-
Ian Collins。
>
[见 http://www.gotw.ca/resources /clcm.htm 有关的信息]
[comp.lang.c ++。moderated。第一次海报:做到这一点! ]
Sounds logical. gcc rejects both lines, Sun CC only barfs on C c = C(42);
--
Ian Collins.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
2月27日凌晨3:35,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:
[...]
On Feb 27, 3:35 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
[...]
class B {
B(const B&);
B& operator =(const B&);
public:
B(int);
};
class C:public B {
public:
C(int);
};
int main(){
C c = C(42); // Comeau:没有错误
}
class B {
B(const B&);
B& operator=(const B&);
public:
B(int);
};
class C : public B {
public:
C(int);
};
int main() {
C c = C(42); // Comeau: no error
}
[...]
[...]
据我所知,临时可以省略,但它的创建应该是可能的(12.2 / 1),好像它没有被省略。
And由于C'的复制c-tor无法创建(12.8 / 7),因此需要(或将要求)的代码
也是格式错误。
我在哪里犯错?或者我呢?
As I understand it, a temporary can be omitted, but its
creation should be possible (12.2/1) as if it weren''t omitted.
And since C''s copy c-tor cannot be created (12.8/7), the code
that requires (or would require) it is also ill-formed.
Where do I err? Or do I?
我相信你是对的。对我来说12.8-7在
主题上显得非常清楚:复制构造函数是定义的,即使它的使用被省略了
,因为它有一个不可访问的基类复制构造函数,程序
格式不正确。
干杯,
Nicola Musatti
-
[见 http:// www。 gotw.ca/resources/clcm.htm 了解有关的信息]
[comp.lang.c ++。moderated。第一次海报:做到这一点! ]
I believe you''re right. To me 12.8-7 appears quite clear on the
subject: The copy constructor is defined even if it''s use is elided
and as it has an inaccessible base class copy constructor, the program
is ill-formed.
Cheers,
Nicola Musatti
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
这篇关于使用无法访问的基类复制c-tor复制构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!