随笔分类 -  cplusplus

摘要:C++语言: Codee#25861001 #include <iostream>002 using namespace std;003 004 /*005 traits 技术006 将某种类型相关联的所有申明绑定在一起的实现方式007 可以以灵活的方法从它们中的语境中将这些的类型和值进行“混合与匹配”008 chm按:在stl的源码中多见此技术的使用,Hjj 先生将其视为一种重要的技术009 */010 class Milk011 {012 public:013 friend ostream& operator<<(ostream& os, const 阅读全文
posted @ 2012-03-17 16:50 strorehouse
摘要:c语言结构体,以前没仔细看,原来对其理解不够。C++语言: Codee#2580101 #include <stdio.h>02 03 struct point04 {05 int x;06 int y;07 float unknown;08 };09 10 struct rect11 {12 struct point pt1;13 struct point pt2;14 };15 16 struct point makepoint(int x, int y)17 {18 struct point temp;19 20 temp.x = x;21 temp.y = y;22 ... 阅读全文
posted @ 2012-03-13 10:00 strorehouse
摘要:C++语言: Codee#2575401 // type_info example02 #include <iostream>03 #include <typeinfo>04 using namespace std;05 06 struct Base {};07 struct Derived : Base {};08 struct Poly_Base09 {10 virtual void Member() {}11 };12 struct Poly_Derived: Poly_Base {};13 14 int main()15 {16 // built-in type 阅读全文
posted @ 2012-03-10 09:48 strorehouse
摘要:C++语言: Codee#2575201 #include <iostream>02 03 using namespace std;04 05 class Trace06 {07 static int counter;08 int objid;09 public:10 Trace()11 {12 objid = ++counter;13 cout << "constructing Trace #" << objid << endl;14 if(objid == 3)15 throw 3;16 }17 ~Trace()18 {1 阅读全文
posted @ 2012-03-09 11:00 strorehouse
摘要:int setjmp ( jmp_buf env );Save calling environment for long jumpThis function with functional form takes its argument,env, and fills its content with information about the environment state in that point in the code to be able to restore that state in a later call tolongjmp.ParametersenvObject of t 阅读全文
posted @ 2012-03-08 16:39 strorehouse
摘要:C++语言: Codee#2574401 /*02 对象切片03 此处为传值 注意与传地址相比较04 */05 #include <iostream>06 #include <string>07 using namespace std;08 09 class Pet10 {11 string pname;12 public:13 Pet(const string& name)14 : pname(name)15 {}16 virtual string name() const17 {18 return pname;19 }20 virtual string de 阅读全文
posted @ 2012-03-08 10:46 strorehouse
摘要:C++语言: Codee#2574201 #include <iostream>02 #include <cstdio>03 using namespace std;04 05 class NotEmpty06 {07 08 };09 10 int main()11 {12 cout<<sizeof(NotEmpty)<<endl;13 return 0;14 }15 16 /*17 118 19 Process returned 0 (0x0) execution time : 0.027 s20 Press any key to contin 阅读全文
posted @ 2012-03-08 09:30 strorehouse
摘要:C++语言: Codee#2574101 /*02 私有继承成员的公有化03 */04 class Pet05 {06 public:07 char eat() const08 {09 return 'a';10 }11 int speak() const12 {13 return 2;14 }15 float sleep() const16 {17 return 3.0;18 }19 float sleep(int) const20 {21 return 4.0;22 }23 };24 25 class Goldfish: Pet //private in... 阅读全文
posted @ 2012-03-07 20:33 strorehouse
摘要:C++语言: Codee#2573601 /*02 构造函数设为私有03 +04 类自身的静态数据成员放到类的内部05 */06 07 #include <iostream>08 using namespace std;09 class Egg10 {11 static Egg e; // it's so strange!12 int i;13 Egg(int ii)14 : i(ii)15 {}16 Egg(const Egg&);17 public:18 static Egg* instance()19 {20 return... 阅读全文
posted @ 2012-03-06 15:32 strorehouse
摘要:C++语言: Codee#2573401 /*02 枚举值不占用对象空间,编译期间得到枚举值03 */04 05 #include <iostream>06 using namespace std;07 08 class Bunch09 {10 enum11 {12 size = 100013 };14 int i[size];15 };16 17 int main()18 {19 cout << "sizeof(bunch) =" << sizeof(Bunch)20 << ",sizeof(i[1000]) = 阅读全文
posted @ 2012-03-06 10:54 strorehouse
摘要:C++语言: Codee#2573001 #include <>iostream>02 using namespace std;03 04 union U05 {06 private:07 int i;08 float f;09 public:10 U(int a);11 U(float b);12 ~U();13 int read_int();14 float read_float();15 };16 17 U::U(int a)18 {19 i = a;20 }21 22 U::U(float b)23 {24 f = b;25 }26 27 U::~U()28 {29 阅读全文
posted @ 2012-03-05 20:43 strorehouse
摘要:C++语言: Codee#2572801 /* 02 table-driven example from <<thinking in cpp>>03 */04 05 06 #include <iostream>07 using namespace std;08 09 #define DF(N) void N(){\10 cout << "function"#N " called..."<<endl;}11 12 DF(a);13 DF(b);14 DF(c);15 DF(d);16 17 voi 阅读全文
posted @ 2012-03-05 10:24 strorehouse