由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 一个c++ constructor的问题, thanks
相关主题
菜鸟请教smart pointerwhat is the difference?
请教个Bloomberg 的 C++ 题目C++ 中 myobject * a =new myobject[n] 的问题
用包子呼唤大牛们--问关于C++Destructor的问题C++里面
请教一个基本的constructor和destrcutor问题几个问题
请教这个程序里用到了什么constructor啊?有几个copy constructor?[合集] 关于C++ default copy constructor
请教一下,exception时,destructor一定会被调用么?[合集] C++问题(copy constructor)
What does the default constructor do?有大侠讲讲RTTI和exception的问题么?
c++ question关于c++的constructor的面试题
相关话题的讨论汇总
话题: foo话题: int话题: object话题: c++
进入Programming版参与讨论
1 (共1页)
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?
相关主题
请教一下,exception时,destructor一定会被调用么?what is the difference?
What does the default constructor do?C++ 中 myobject * a =new myobject[n] 的问题
c++ questionC++里面
进入Programming版参与讨论
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;

1 (共1页)
进入Programming版参与讨论
相关主题
关于c++的constructor的面试题请教这个程序里用到了什么constructor啊?有几个copy constructor?
包含指针的类和vector的问题请教一下,exception时,destructor一定会被调用么?
What is wrong with the constructor calling?What does the default constructor do?
关于C++ copy-constructor 一个奇怪的问题c++ question
菜鸟请教smart pointerwhat is the difference?
请教个Bloomberg 的 C++ 题目C++ 中 myobject * a =new myobject[n] 的问题
用包子呼唤大牛们--问关于C++Destructor的问题C++里面
请教一个基本的constructor和destrcutor问题几个问题
相关话题的讨论汇总
话题: foo话题: int话题: object话题: c++