65,210
社区成员
发帖
与我相关
我的任务
分享
Student::Student()
{
name = new char[30];
if(name!=NULL)
{
strcpy(name,"c++ class study");
}
}
int main()
{
Student s1,s2(s1);
cout<<"输出s1"<<endl;
s1.outputName();
cout<<"输出s2"<<endl;
s2.outputName();
s2.setName("codeblock");
cout<<"输出s2"<<endl;
s2.outputName();
cout<<"输出s1"<<endl;
s1.outputName();
return 0;
}


多用小脑和手,少用大脑、眼睛和嘴,会更快地学会编程!
眼过千遍不如手过一遍!
书看千行不如手敲一行!
手敲千行不如单步一行!
单步源代码千行不如单步Debug版对应汇编一行!
单步Debug版对应汇编千行不如单步Release版对应汇编一行!
不会单步Release版对应汇编?在你想单步Release版C/C++代码片断的前面临时加一句DebugBreak();重建所有,然后在IDE中运行。(一般人我不告诉他!
)
单步类的实例“构造”或“复制”或“作为函数参数”或“作为函数返回值返回”或“参加各种运算”或“退出作用域”的语句对应的汇编代码几步后,就会来到该类的“构造函数”或“复制构造函数”或“运算符重载”或“析构函数”对应的C/C++源代码处。
VC调试时按Alt+8、Alt+7、Alt+6和Alt+5,打开汇编窗口、堆栈窗口、内存窗口和寄存器窗口看每句C对应的汇编、单步执行并观察相应堆栈、内存和寄存器变化,这样过一遍不就啥都明白了吗。
对VC来说,所谓‘调试时’就是编译连接通过以后,按F10或F11键单步执行一步以后的时候,或者在某行按F9设了断点后按F5执行停在该断点处的时候。
《深度探索C++对象模型》
《C++反汇编与逆向分析技术揭秘》

#include <iostream>
#include <string.h>
using namespace std;
class Student
{
public:
Student();
void setName(char *n){};
void outputName()
{
cout << "name_part " << name_part << endl;
cout << "name_mem " << name_mem << endl;
};
private:
char *name_part;
char *name_mem;
char m_xx[30];
};
Student::Student()
{
char xx[30]; //局部变量,初始化结束后就销毁了,所以初始化完成后name指向的是被销毁的内存
name_part = xx;
name_mem = m_xx;
// name = new char[30];
if(name_part!=NULL &&name_mem!=NULL)
{
strcpy(name_part,"c++ class study");
strcpy(name_mem,"c++ class study");
}
}
int main()
{
Student s1,s2(s1);
cout<<"输出s1"<<endl;
s1.outputName();
cout<<"输出s2"<<endl;
s2.outputName();
s2.setName("codeblock");
cout<<"输出s2"<<endl;
s2.outputName();
cout<<"输出s1"<<endl;
s1.outputName();
return 0;
}

class Student
{
public:
Student();
void setName(char *n);
void outputName();
private:
char *name;
};
2.xx作为数组名,理应该会把地址传给name
Student::Student()
{
char xx[30];
name = xx;
// name = new char[30];
if(name!=NULL)
{
strcpy(name,"c++ class study");
}
}