[C++应用程序性能优化]对象的生命周期
来源:互联网 发布:钢琴谱制作软件 编辑:程序博客网 时间:2024/06/05 14:36
#include "stdafx.h"#include <stdio.h>#include <stdlib.h>class A{public: A() { printf("A created.\n"); } ~A() { printf("A destroyed.\n"); }};class B{public: B() { printf("B created.\n"); } ~B() { printf("B destroyed.\n"); }};A g_A;int foo(){ printf("\nfoo() ----------------->\n"); A localA; static B localB; printf("\nfoo() <-----------------\n"); return 0;}int main(){ printf("main() ------------------>\n"); foo(); foo(); printf("\nmain()<----------------\n"); system("pause"); return 0;}#include "stdafx.h"#include <stdio.h>#include <stdlib.h>class A{public: A() { printf("A created.\n"); } ~A() { printf("A destroyed.\n"); }};A* createA(){ A* p = new A(); return p;}void deleteA(A* p){ delete p;}int main(){ A *pA = createA(); pA = createA(); deleteA(pA); system("pause"); return 0;} #include "stdafx.h"#include <stdio.h>#include <stdlib.h>class A{public: A() { printf("A created.\n"); } A(A& a) { printf("A created with copy\n"); } ~A() { printf("A destroyed.\n"); }};A foo(A a){ A b; return b;}int main(){ A a; a = foo(a); system("pause"); return 0;}