B*****t 发帖数: 335 | 1 找出下面的程序的毛病!
class Foo {
public:
Foo(int x, int y){
xx = x, yy = y;
}
Foo(int x) {
new(this) Foo(x*2, x*3);
}
int xx,yy;
};
int main() {
Foo fo(3);
cout<
cout<
return 0;
} | p***o 发帖数: 1252 | 2 Seems legal, unless Foo has non-POD members or bases.
【在 B*****t 的大作中提到】 : 找出下面的程序的毛病! : class Foo { : public: : Foo(int x, int y){ : xx = x, yy = y; : } : Foo(int x) { : new(this) Foo(x*2, x*3); : } : int xx,yy;
| S*********g 发帖数: 5298 | 3 http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.3
【在 B*****t 的大作中提到】 : 找出下面的程序的毛病! : class Foo { : public: : Foo(int x, int y){ : xx = x, yy = y; : } : Foo(int x) { : new(this) Foo(x*2, x*3); : } : int xx,yy;
| z****e 发帖数: 2024 | 4 这个link也没说是怎么回事。只是说很不好,但是没说绝对不行。
【在 S*********g 的大作中提到】 : http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.3
| e**u 发帖数: 409 | 5 可能是因为那个Foo(x)里面的new会调用什么拷贝构造函数啥的吧
【在 z****e 的大作中提到】 : 这个link也没说是怎么回事。只是说很不好,但是没说绝对不行。
| X****r 发帖数: 3557 | 6 I think it has to do with the lifetime of an object:
In [basic.life]
"""
The lifetime of an object of type T ends when:
--if T is a class type with a non-trivial destructor (_class.dtor_),
the destructor call starts, or
--the storage which the object occupies is reused or released.
"""
Calling placement new on 'this' reuses the storage for another
object, thus the lifetime of the original object ends. Accessing
members of the original object would result in undefined behavior.
Of cour
【在 z****e 的大作中提到】 : 这个link也没说是怎么回事。只是说很不好,但是没说绝对不行。
| B*****t 发帖数: 335 | 7 what does POD stand for?
【在 p***o 的大作中提到】 : Seems legal, unless Foo has non-POD members or bases.
| B*****t 发帖数: 335 | 8 Thanks a lot!
so you mean that if the compiler doesn't change the content of the object,
we can use placement new in one constructor to call another constructor
without any logical error?
【在 X****r 的大作中提到】 : I think it has to do with the lifetime of an object: : In [basic.life] : """ : The lifetime of an object of type T ends when: : --if T is a class type with a non-trivial destructor (_class.dtor_), : the destructor call starts, or : --the storage which the object occupies is reused or released. : """ : Calling placement new on 'this' reuses the storage for another : object, thus the lifetime of the original object ends. Accessing
| a****l 发帖数: 8211 | 9 我现在开始理解为什么以前的人要发明java了. c/c++给了程序员太多的自由,然后马上
就有人开始用各种希奇古怪的方法来做一些完全不需要的事最后产生各种预料不到的问
题了.以前的人说c是"shoot your own feet with a gun",现在看来是描绘的太形象了.
我对这种问题的看法是"不要听,不要说,不要想".
【在 B*****t 的大作中提到】 : 找出下面的程序的毛病! : class Foo { : public: : Foo(int x, int y){ : xx = x, yy = y; : } : Foo(int x) { : new(this) Foo(x*2, x*3); : } : int xx,yy;
| p***o 发帖数: 1252 | 10 plain old data, roughly any class/struct without any constructors
【在 B*****t 的大作中提到】 : what does POD stand for?
| | | c********g 发帖数: 30 | 11
不理解为什么要这样写?为什么不
Foo(int x) xx(x*2), yy(x*3) : {}
【在 B*****t 的大作中提到】 : 找出下面的程序的毛病! : class Foo { : public: : Foo(int x, int y){ : xx = x, yy = y; : } : Foo(int x) { : new(this) Foo(x*2, x*3); : } : int xx,yy;
| X****r 发帖数: 3557 | 12 Just don't do it. There is no point to complicate the code without much gain
like this, even it is legal, which it probably is not.
【在 B*****t 的大作中提到】 : Thanks a lot! : so you mean that if the compiler doesn't change the content of the object, : we can use placement new in one constructor to call another constructor : without any logical error?
| d****p 发帖数: 685 | 13 The motivation behind the code is justifiable - copy constructor delegation,
a feature implemented in other languages, such as Objective-C.
Basically, you want to call another constructor, which is normally more
generic, when defining a more customized ctor. This feature will be coming
in c++0x.
Of course, the placement new is not a good thing for general use.
【在 B*****t 的大作中提到】 : 找出下面的程序的毛病! : class Foo { : public: : Foo(int x, int y){ : xx = x, yy = y; : } : Foo(int x) { : new(this) Foo(x*2, x*3); : } : int xx,yy;
| B*****t 发帖数: 335 | 14 Thanks!
delegation,
【在 d****p 的大作中提到】 : The motivation behind the code is justifiable - copy constructor delegation, : a feature implemented in other languages, such as Objective-C. : Basically, you want to call another constructor, which is normally more : generic, when defining a more customized ctor. This feature will be coming : in c++0x. : Of course, the placement new is not a good thing for general use.
| h*****0 发帖数: 4889 | 15 undefined behavior 吧
new一个对象和在栈上开一个对象,内存模型可能会有不同,那就死掉了。
【在 B*****t 的大作中提到】 : 找出下面的程序的毛病! : class Foo { : public: : Foo(int x, int y){ : xx = x, yy = y; : } : Foo(int x) { : new(this) Foo(x*2, x*3); : } : int xx,yy;
|
|