由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - What is wrong with the constructor calling?
相关主题
问个char*的问题What is wrong with the code?
private destructor谁给新手解释一下这个c++小程序
[合集] C++问题(copy constructor)请教c++的string vector问题,谢谢! (转载)
请教一个基本的constructor和destrcutor问题question regarding effective c++ by Meyers
What is wrong with the constructor (or the code)?请教个Bloomberg 的 C++ 题目
c++环境入门问题a c++ question.
pointer to functionArray in C
一个指向指针的指针的引用?一个c++ constructor的问题, thanks
相关话题的讨论汇总
话题: wrong话题: char话题: what话题: a2
进入Programming版参与讨论
1 (共1页)
S*****H
发帖数: 90
1
In the following code, why a3 is wrong, but a2 is wrong?
#include
using namespace std;
class A{
char * word;
public:
//Read this constructor carefully
A(char * cp){ word = cp;}
~A(){delete[] word;}
};
int main(){
char w[] = "spam";
char * wp = new char[5];
strcpy(wp, w);
A a2 = A(w);
A a3 = A(wp);
return 0;
}
H***a
发帖数: 735
2
"why a3 is wrong, but a2 is wrong"? Is that your question? Please check your
question before asking.
I confirmed that a3 works but a2 doesn't, because the destructor of A is
trying to delete the array allocated on the stack rather than on the heap, which
is forbidden.
S*****H
发帖数: 90
3
Hyena (大石头),
You are right. I mis-typed the question. The results are just
the opposite.
Thank you for your answer very much!
1 (共1页)
进入Programming版参与讨论
相关主题
一个c++ constructor的问题, thanksWhat is wrong with the constructor (or the code)?
菜鸟请教smart pointerc++环境入门问题
3 c++ challenge-and-grill questionspointer to function
Why C++11 introduce "decltype" and "auto"?一个指向指针的指针的引用?
问个char*的问题What is wrong with the code?
private destructor谁给新手解释一下这个c++小程序
[合集] C++问题(copy constructor)请教c++的string vector问题,谢谢! (转载)
请教一个基本的constructor和destrcutor问题question regarding effective c++ by Meyers
相关话题的讨论汇总
话题: wrong话题: char话题: what话题: a2