每个人
我都有一些java经验,并且是c ++的初学者.
belw是我的代码,它的输出是
0 1 2 3 4 5 6 7 8 9
destructor ---s1
8791616 8785704 2
destructor ---s1
Run Code Online (Sandbox Code Playgroud)
我预计输出是
0 1 2 3 4 5 6 7 8 9
destructor ---abc
0 1 2
destructor ---s1
Run Code Online (Sandbox Code Playgroud)
我无法理解为什么析构函数释放第一个对象的资源.如何打印我预期的输出?
#include <iostream>
using namespace std;
class Sequence{
public:
Sequence(int count=10,string name = "abc");
void show();
~Sequence();
int* _content;
int _count;
string _name;
};
Sequence::Sequence(int count,string name){
_count = count;
_content=new int[count];
_name = name;
for(int i=0;i<count;i++){
_content[i]=i;
}
}
Sequence::~Sequence(){ …Run Code Online (Sandbox Code Playgroud) 我的代码如下所示,当我编译它时,我收到此错误:
two.cpp:5:错误:在'='标记之前的预期构造函数,析构函数或类型转换
#include <iostream>
using namespace std;
namespace a1{
int a=3;
a=4;
}
int main(){
cout << a1::a<<endl;
}
Run Code Online (Sandbox Code Playgroud)
我在两个文件中定义了一个命名空间时遇到了这个问题,在第二个文件中,我无法为第一个文件中定义的变量赋值.
我正在学习初学ANSI C++,并且在书中找不到任何关于此的信息.