steam网站代做,网站开发高级证,网络营销专业可以干什么工作,网站开发开源的手册欢迎转载#xff0c;但请标明作者 “九天雁翎”#xff0c;当然#xff0c;你给出这个帖子的链接更好。 不知不觉我都写了6讲了#xff0c;的确这样讲出来的学习才能迫使我真的去调试每个书上出现的代码#xff0c;去想些自己能讲出什么新的书上没有的东西#xff0c;这才… 欢迎转载但请标明作者 “九天雁翎”当然你给出这个帖子的链接更好。 不知不觉我都写了6讲了的确这样讲出来的学习才能迫使我真的去调试每个书上出现的代码去想些自己能讲出什么新的书上没有的东西这才是真的学习吧以前看完书做道题式的就以为自己基本都掌握了在类这里好像行不通因为我的C基础不适合这里。。。。呵呵不说题外话了。这次讲析构函数相对于构造函数。析构函数就是在类的声明周期结束的时候运行的函数一般用来把一个类的资源释放出来的家伙。就我了解JAVA好像不需要这样的工作他们有垃圾回收器我看一个比较理性的程序员评价这种情况是有利有弊类似的简化让JAVA成为了最佳商业模式开发软件但是让JAVA程序员太脱离底层让他们的知识匮乏因为编JAVA不需要那么多知识而且让JAVA失去了很多底层应用。另外这样的垃圾回收是耗资源的当时Bjarne Strooustrup就考虑过也给C加一个这样的特性但他又说作为一个系统开发级及常用来开发驱动程序的语言他无法接受那样的效率损失。所以最后C没有加上这个特性。又讲多了看析构函数吧。 例7.0 #include string #include iostream using namespace std; bool being true; //定义一个全局bool变量 class Fruit //定义一个类名字叫Fruit { string name; //定义一个name成员 string colour; //定义一个colour成员 public: void print() //定义一个输出名字的成员print() { coutcolour nameendl; } Fruit(const string nst apple,const string cst green):name(nst),colour(cst) { being true; //表示这个东西存在 } //构造函数 ~Fruit() //这就是传说中的析构函数 { being false; //表示他不存在了 } }; int main() { { Fruit apple(apple); //定义一个Fruit类对象apple cout apple being?: beingendl; } cout apple being?: beingendl; return 0; } 首先看到不要惊讶 我们的构造函数和析构函数都作了些什么啊。我说过构造函数就是构造一个类对象会运行的函数析构函数就是类生命周期结束时运行时运行的函数不仅仅是我们的一般理解啊从逻辑上来讲他们可以DO Everything你首先要知道他们能干什么啊而且还要知道他们什么时候起作用因为我们用一个大括号把apple的定义括起来了在大括号消失的时候apple就需要消失了于是这时候调用了析构函数。下面我们先看看可以做什么的例子。 例7.1 #include string #include iostream using namespace std; bool being true; class Fruit //定义一个类名字叫Fruit { string name; //定义一个name成员 string colour; //定义一个colour成员 public: void print() //定义一个输出名字的成员print() { coutcolour nameendl; } Fruit(const string nst apple,const string cst green):name(nst),colour(cst) { being true; cout Aha,Im name. I have created!endl; } //构造函数 ~Fruit() { being false; cout Dame it!Im name. And who killed me?endl; } }; int main() { { Fruit apple(apple); //定义一个Fruit类对象apple cout apple being?: beingendl; } cout apple being?: beingendl; return 0; } 你运行看看就知道了在一个对象定义的时候他会高呼自己被创造了当他消失的时候他会宣布自己的死亡好的Fruit的对象看起来已经知道自己什么时候有生命了让我们来看看到底什么时候吧。 例7.2 #include string #include iostream using namespace std; bool being true; class Fruit //定义一个类名字叫Fruit { string name; //定义一个name成员 string colour; //定义一个colour成员 public: void print() //定义一个输出名字的成员print() { coutcolour nameendl; } Fruit(const string nst apple,const string cst green):name(nst),colour(cst) { being true; cout Aha,Im name. I have created!endl; } //构造函数 ~Fruit() { being false; cout Dame it!Im name. And who killed me?endl; } }; Fruit banana(banana); void Fb() { cout我是一个函数的开始endl; Fruit pear(pear); cout我是一个函数的结束endl; } int main() { cout我是程序的开始endl; Fb(); cout我是for循环的开始endl; for(bool bi true;bi;bifalse) { Fruit orange(orange); cout我是for循环的结束endl; } { cout我是语句块的开始endl; Fruit apple; //第一种情况语句块中创建。 cout我是语句块的结束endl; } cout我是程序的结束endl; return 0; } 就这个程序运行的情况来看一个类的生命周期和一个普通变量的生命周期类似全局变量最先创建程序结束时结束函数体内的变量调用时创建函数结束时结束for循环内的变量在for循环结束时结束语句块内的变量在语句块结束时结束。本来Bjarne stroustrup就宣称他希望让类就像内置类型一样使用看来他不是说着好玩的这里要说明的是即使你没有定义析构函数系统也会像定义默认构造函数一样帮你定义一个。让我们看看什么时候需要析构函数呢 例7.3 #include string #include iostream using namespace std; class Fruit //定义一个类名字叫Fruit { string name; //定义一个name成员 string colour; //定义一个colour成员 public: void print() //定义一个输出名字的成员print() { coutcolour nameendl; } Fruit(const string nst apple,const string cst green):name(nst),colour(cst) { cout Aha,Im name. I have created!endl; } //构造函数 Fruit(Fruit aF) //还记得我吗?我是复制构造函数 { name another aF.name; } ~Fruit() { cout Dame it!Im name. And who killed me?endl; } }; int main() { coutmain beginendl; coutcreated *Pendl; { Fruit *p new Fruit; coutcreated another appleendl; Fruit apple(*p); } coutmain endendl; return 0; } 运行这个程序你发现什么了对首先运行复制构造函数就不运行构造函数了因为another apple没有宣布自己的诞生其次当语句块消失的时候another apple自动调用了析构函数他宣布他“死”了但是动态创建的由*p指向的对象虽然宣布自己诞生了但是却重来没有宣布自己死过哪怕程序结束了也是这样不知道vc有没有回收内存的措施不然我甚至怀疑你要是重复调试这个程序可以使你的机子崩溃当然假如可以的话将不知道需要多少次但是理论上确实可以的。这就是内存泄露作为一个C程序员需要了解的东西比一个JAVA程序员要多的多回报是你能做的事情也多地多这就是你需要记住的一个动态创建的对象记得手动把它撤销。就像下面的例子一样。 例7.4 #include string #include iostream using namespace std; class Fruit //定义一个类名字叫Fruit { string name; //定义一个name成员 string colour; //定义一个colour成员 public: void print() //定义一个输出名字的成员print() { coutcolour nameendl; } Fruit(const string nst apple,const string cst green):name(nst),colour(cst) { cout Aha,Im name. I have created!endl; } //构造函数 Fruit(Fruit aF) //还记得我吗?我是复制构造函数 { name another aF.name; } ~Fruit() { cout Dame it!Im name. And who killed me?endl; } }; int main() { coutmain beginendl; coutcreated *Pendl; { Fruit *p new Fruit; coutcreated another appleendl; Fruit apple(*p); coutdelete pendl; delete p; } coutmain endendl; return 0; } 这样才能保证你的机子不会崩溃。当你删除指针的时候系统帮你自动调用了对象的析构函数假如上面的例子还不能摧毁你对自己自己内存足够大的信心的话看下面的例子 例7.5 #include string #include iostream using namespace std; class Fruit //定义一个类名字叫Fruit { string name; //定义一个name成员 string colour; //定义一个colour成员 public: void print() //定义一个输出名字的成员print() { coutcolour nameendl; } Fruit(const string nst apple,const string cst green):name(nst),colour(cst) { cout Aha,Im name. I have created!endl; } //构造函数 Fruit(Fruit aF) //还记得我吗?我是复制构造函数 { name another aF.name; } ~Fruit() { cout Dame it!Im name. And who killed me?endl; } }; int main() { coutmain beginendl; coutcreated *Pendl; { Fruit *p new Fruit[10]; coutcreated another appleendl; Fruit apple(*p); coutdelete pendl; delete []p; } coutmain endendl; return 0; } 你会发现创建一个对象的数组时分别为每一个调用了构造函数删除一个动态数组对象的时候系统帮你自动为每一个调用了析构函数还不了赖嘛但是别忘了p前面的[]表示这是个数组更别忘了删除它你可以把10改成更大的数并不删除它来尝试一下呵呵。析构函数就讲到这里罗。