网游之全职天下,周姿君,yy年度盛典2015视频
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <fstream> using namespace std; class abcd { public: abcd(int a, int b, int c) { _a = a; _b = b; _c = c; printf("abcd() construct, a: %d,b: %d,c: %d \n", _a, _b, _c); } ~abcd() { printf("~abcd() construct,a: %d,b: %d,c: %d \n", _a, _b, _c); } int geta() { return _a; } private: int _a; int _b; int _c; }; class mye { public: mye() :abcd1(1, 2, 3), abcd2(4, 5, 6), m(100) { cout << "mye()" << endl; } ~mye() { cout << "~mye()" << endl; } mye(const mye & obj) :abcd1(7, 8, 9), abcd2(10, 11, 12), m(100) { printf("myd(const myd & obj) \n"); } public: abcd abcd1; //c++编译器不知道如何构造abc1 abcd abcd2; const int m; }; int dothing(mye mye1)//mye1.拷贝构造(main::mye) { printf("dothing() mye1.abc1.a: %d \n", mye1.abcd1.geta()); return 0; } int run() { mye mye; dothing(mye); return 0; } int run2() { printf("run2 start.. \n"); //abcd(400, 500, 600); //临时对象的⽣命周期 abcd abcd = abcd(100, 200, 300); printf("run2 end\n"); return 0; } int main(void) { run2(); return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <fstream> using namespace std; //构造中调⽤构造是危险的⾏为 class mytest { public: mytest(int a, int b, int c) { _a = a; _b = b; _c = c; } mytest(int a, int b) { _a = a; _b = b; mytest(a, b, 100);//创建一个匿名对象 // } ~mytest() { printf("mytest~: %d, %d, %d\n", _a, _b, _c); } int getc() { return _c; } void setc(int val) { _c = val; } private: int _a; int _b; int _c; }; int main() { mytest t1(1, 2); printf("c: %d\n", t1.getc()); //请问c的值是? return 0; }
c与c++的比较
#define _crt_secure_no_warnings #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <fstream> using namespace std; class test { public: test() { cout << "test()" << endl; m_a = 0; m_b = 0; } test(int a, int b) { cout << "test(int, int)" << endl; m_a = a; m_b = b; } void printt() { cout << "printt:"<<m_a<<","<<m_b << endl; } ~test() { cout << "~test()" << endl; } private: int m_a; int m_b; }; //c语言中 void test1() { int *p = (int *)malloc(sizeof(int)); *p = 10; if (p != null) { free(p); //delete p; p = null; } int *array_p = (int *)malloc(sizeof(int)* 10); for (int i = 0; i < 10; i++) { array_p[i] = i + 1; } for (int i = 0; i < 10; i++) { printf("%d ", array_p[i]); } printf("\n"); if (array_p != null) { free(array_p); array_p = null; } cout << "==============" << endl; test *tp = (test*)malloc(sizeof(test)); tp->printt(); if (tp != null) { free(tp); tp = null; } } //malloc free 是函数,标准库,stdlib.h //new 在堆上初始化一个对象的时候,会触发对象的构造函数。malloc不能 //free并不能触发一个对象的析构函数。 //c++中 void test2() { int *p = new int; *p = 10; if (p != null) { free(p); p = null; } int *array_p = new int[10]; for (int i = 0; i < 10; i++) { array_p[i] = i + 1; } for (int i = 0; i < 10; i++) { cout << array_p[i]<<" "; } cout << endl; if (array_p != null) { delete [] array_p; } cout << "==========" << endl; //test *tp = new test(10, 20);//触发有参构造 test *tp = new test;//触发无惨构造 tp->printt(); if (tp != null) { delete tp; tp = null; } } int main(void) { test1(); cout << "-----------" << endl; test2(); return 0; }
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
如何在没有core文件的情况下用dmesg+addr2line定位段错误
用QT制作3D点云显示器——QtDataVisualization
网友评论