复制构造函数和printf [英] copy constructors and printf

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

问题描述

如果按值将C ++对象作为参数传递给具有

a可变长度参数列表(...)的函数,则MSVC7 C ++编译器不会

调用对象的复制构造函数,如果复制

构造函数是私有的,则不会抱怨。


1)这部分是C ++语言定义?什么是思考

背后呢?


2)在任何情况下,我怎样才能在编译时捕获任何试图使用的客户? b $ b按值将C ++对象传递给像printf这样的函数?


------------------------ -------------------------

Samuel W. Wilson Bentley Systems,Inc。
sa ******** @ bentley.com www.bentley.com

解决方案



" ; Sam Wilson [Bentley]" < SA ******** @ bentley.com>在消息中写道

news:40 *************** @bentley.com ...

如果你传递一个C ++对象通过值作为具有可变长度参数列表(...)的函数的参数,MSVC7 C ++编译器不会调用对象的复制构造函数,并且如果副本
构造函数是私有的,则不要抱怨。

1)这是C ++语言定义的一部分吗?背后的想法是什么?


有点,C ++标准只是说这种行为是未定义的。


在这种情况下调用复制构造函数没有意义,为什么不调用一个

转换运算符,或者一个

不同类的一个参数非显式构造函数,所有这些都是正确的。基本上,当你不知道被调用函数的类型是什么时,你不能做正确的事情

(可能与提供的类型不一样)。

2)在任何情况下,如何在编译时捕获任何尝试按值将C ++对象传递给printf等函数的调用者?




你不能。如果这是一个问题,那么请使用类型安全的IO库,例如

作为标准的iostream库。


john




" John Harrison" <乔************* @ hotmail.com>在留言中写道

news:c6 ************ @ ID-196037.news.uni-berlin.de ...


Sam Wilson [Bentley]" < SA ******** @ bentley.com>在消息中写道
新闻:40 *************** @bentley.com ...

如果按值传递C ++对象作为对于具有可变长度参数列表(...)的函数的参数,MSVC7 C ++编译器不会调用该对象的复制构造函数,并且如果copy
构造函数是私有的。

1)这是C ++语言定义的一部分吗?背后的想法是什么?
有点,C ++标准只是说这种行为是未定义的。

在这种情况下调用复制构造函数没有意义




为什么?我不明白这个解释。编译器明确地将

作为对象的副本 - 为什么它不会调用复制构造函数?我可以看到

为什么它不会,不能,也不应该调用任何其他类型的

转换为未知类型。但制作副本正在制作副本,为什么

这种情况​​下特殊?


如果这就是它的方式,那么是的,但我似乎并不认为必须或纠正它是一个未定义的东西。语言中的漏洞。

,为什么不调用
转换运算符,或者将
的一个参数非显式构造函数称为另一个类,所有这些都是正确的。基本上,当你不知道被调用函数的类型是什么时,你不能做
正确的事情(这可能与提供的类型不同)。


2)在任何情况下,如何在编译时捕获任何尝试按值将C ++对象传递给printf之类的函数的调用者?
你不能。如果这是一个问题,那么使用类型安全的IO库,



,例如标准的iostream库。


因为它不仅仅是具有varargs功能的IO库。

john




Keith




" Keith" <柯*********** @ bentley.com>在消息中写道

news:c6 ********** @ news.bentley.com ...


John Harrison <乔************* @ hotmail.com>在消息中写道
新闻:c6 ************ @ ID-196037.news.uni-berlin.de ...


" ; Sam Wilson [Bentley]" < SA ******** @ bentley.com>在消息中写道
新闻:40 *************** @bentley.com ...

如果按值传递C ++对象作为对于
具有可变长度参数列表(...)的函数的参数,MSVC7 C ++编译器不会调用对象的复制构造函数,并且如果copy
构造函数是私有的。

1)这是C ++语言定义的一部分吗?背后的想法是什么?
有点,C ++标准只是说这种行为是未定义的。

在这种情况下调用复制构造函数没有意义



为什么?我不明白这个解释。编译器显式地



制作对象的副本 - 为什么不调用复制构造函数?我可以
看看为什么它不会,不能,也不应该把任何其他的暗示类型转换为未知类型。但制作副本正在制作副本,为什么这个案例是特殊的?


很明显它很特别,因为printf是一个可变函数。你得到

与其他任何可变函数相同的行为。
如果那就是它的方式,那就是它,但我似乎并不是<必要或纠正它是未定义的语言中的漏洞。




我猜C ++的设计者制定了最低标准兼容

与C并不鼓励类型不安全节目。我不确定你提出的建议是否可行,但我认为没有什么需要。


john


If you pass a C++ object by value as an argument to a function which has

a variable-length argument list (...), the MSVC7 C++ compiler does not
call the object''s copy constructor and will not complain if the copy
constructor is private.

1) Is this part of the C++ language definition? What is the thinking
behind it?

2) In any case, how can I catch at compile time any callers that try to
pass C++ objects by value to a function like printf?

-------------------------------------------------
Samuel W. Wilson Bentley Systems, Inc.
sa********@bentley.com www.bentley.com

解决方案


"Sam Wilson [Bentley]" <sa********@bentley.com> wrote in message
news:40***************@bentley.com...

If you pass a C++ object by value as an argument to a function which has

a variable-length argument list (...), the MSVC7 C++ compiler does not
call the object''s copy constructor and will not complain if the copy
constructor is private.

1) Is this part of the C++ language definition? What is the thinking
behind it?
Kind of, the C++ standard just says that such behaviour is undefined.

Its not meaningful to call a copy constructor in this case, why not call a
conversion operator instead, or a one argument non-explicit constructor of a
different class, all of these could be correct. Basically you cannot do the
right thing when you don''t know what type the called function is expecting
(which may not be the same as the type supplied).

2) In any case, how can I catch at compile time any callers that try to
pass C++ objects by value to a function like printf?



You can''t. If this is an issue then use a type safe IO library instead, such
as the standard iostream library.

john



"John Harrison" <jo*************@hotmail.com> wrote in message
news:c6************@ID-196037.news.uni-berlin.de...


"Sam Wilson [Bentley]" <sa********@bentley.com> wrote in message
news:40***************@bentley.com...

If you pass a C++ object by value as an argument to a function which has

a variable-length argument list (...), the MSVC7 C++ compiler does not
call the object''s copy constructor and will not complain if the copy
constructor is private.

1) Is this part of the C++ language definition? What is the thinking
behind it?
Kind of, the C++ standard just says that such behaviour is undefined.

Its not meaningful to call a copy constructor in this case



Why? I don''t understand that explanation. The compiler is explicitly making
a copy of the object - why would it not call the copy constructor? I can see
why it wouldn''t, couldn''t, and shouldn''t call any other implict type
conversion to an unknown type. But making a copy is making a copy, why is
this case "special"?

If that''s just the way it is, so be it, but it doesn''t seem to me to be
necessary or correct that it be an "undefined" hole in the language.
, why not call a
conversion operator instead, or a one argument non-explicit constructor of a different class, all of these could be correct. Basically you cannot do the right thing when you don''t know what type the called function is expecting
(which may not be the same as the type supplied).


2) In any case, how can I catch at compile time any callers that try to
pass C++ objects by value to a function like printf?
You can''t. If this is an issue then use a type safe IO library instead,


such as the standard iostream library.
Because it isn''t just IO libraries that have varargs functions.
john



Keith



"Keith" <ke***********@bentley.com> wrote in message
news:c6**********@news.bentley.com...


"John Harrison" <jo*************@hotmail.com> wrote in message
news:c6************@ID-196037.news.uni-berlin.de...


"Sam Wilson [Bentley]" <sa********@bentley.com> wrote in message
news:40***************@bentley.com...

If you pass a C++ object by value as an argument to a function which has
a variable-length argument list (...), the MSVC7 C++ compiler does not
call the object''s copy constructor and will not complain if the copy
constructor is private.

1) Is this part of the C++ language definition? What is the thinking
behind it?
Kind of, the C++ standard just says that such behaviour is undefined.

Its not meaningful to call a copy constructor in this case



Why? I don''t understand that explanation. The compiler is explicitly


making a copy of the object - why would it not call the copy constructor? I can see why it wouldn''t, couldn''t, and shouldn''t call any other implict type
conversion to an unknown type. But making a copy is making a copy, why is
this case "special"?
Well objviously it special because printf is a variadic function. You get
the same behaviour with any other variadic function.
If that''s just the way it is, so be it, but it doesn''t seem to me to be
necessary or correct that it be an "undefined" hole in the language.



I guess the designers of C++ made the minimum standards to be compatible
with C and not to encourage type unsafe programming. I''m not sure if what
your proposing is workable or not but I don''t see any great need for it.

john


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

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