随笔分类 -  技术-编程-C/C++

摘要:#include #include using namespace std; #define BIT(n) (0x1=0; i--) { if (c & BIT(i)) cout << "1"; else cout << "0"; } cout <<"b" << endl; } int m... 阅读全文
posted @ 2018-12-23 18:55 super行者
摘要:#include #include using namespace std; bool checkLittleEnd (void) { union test { int a; char b; } t; t.a = 1; return (t.b==1); } int main() { if (checkLittle... 阅读全文
posted @ 2018-12-23 18:53 super行者
摘要:【自我总结】 1.默认构造函数不仅可以是无参的,也可以是有参的,但所有参数必须指定默认值。一个类只能有一个默认构造函数。 2.什么时候调用默认构造函数? a.声明类的对象时没有括号时。如:classA objA; b.子类构造函数没有显式调用父类构造函数时 3.构造函数中的默认参数要从右向左指定。 阅读全文
posted @ 2018-12-23 18:49 super行者
摘要:#include #include using namespace std; class demo { public: static int i; // 这里不可以初始化非const static变量! static const int j = 1; // 1st place to initiate for const static varia... 阅读全文
posted @ 2018-12-23 18:48 super行者
摘要:#include #include using namespace std; class demo { public: static void showObjCount(void); private: static int count; static const int j = 1; public: void funcObj(void); demo(i... 阅读全文
posted @ 2018-12-23 18:47 super行者
摘要:class EMPTY { public: EMPTY(); //默认构造函数 EMPTY(const EMPTY &); //复制构造函数 ~EMPTY();//默认析构函数 EMPTY & operator=(const EMTPY &); //赋值运算符 EMPTY * operator&(); //取址运算符 const EMPTY * ... 阅读全文
posted @ 2018-12-23 18:46 super行者
摘要:普通构造函数可以被隐式调用,而explicit构造函数(显式构造函数)只能被显式调用 关于隐式转换,参考:【转】这篇文章 首先, C++中的explicit关键字只能用于修饰只有一个参数的类构造函数, 它的作用是表明该构造函数是显示的, 而非隐式的, 跟它相对应的另一个关键字是implicit, 意 阅读全文
posted @ 2018-12-23 18:44 super行者
摘要:复制构造函数应用场景: 1.一个对象以值传递的方式传入函数体 2.一个对象以值传递方式从函数返回 3.一个对象需要通过另外一个对象进行初始化 阅读全文
posted @ 2018-12-23 18:42 super行者
摘要:如果复制的对象中引用了某个外部的内容(例如分配在堆上的数据),那么在复制这个对象的时候,让新旧两个对象指向同一个外部的内容,就是浅复制;如果在复制这个对象的时候为新对象制作了外部对象的独立的COPY,就是深复制。 深拷贝和浅拷贝主要是针对类中的指针和动态分配的空间来说的,因为对于指针只是简单的值复制 阅读全文
posted @ 2018-12-23 18:40 super行者
摘要:A & operator= (A & b) {} A obj1; A obj2(1,2); obj1 = obj2; //这条赋值语句可理解如下: obj1.=(obj2) , 相当于obj1调用了它自己的=(A &b) 函数, 而=就是函数名。 因此obj1=obj2这句函数调用本身是有返回值的, 阅读全文
posted @ 2018-12-23 18:36 super行者
摘要:全部模板特例化 模板中所有参数全被指定为确定的类型 部分模板特例化分两种情况 1.对部分模板参数进行特例化 2.使用具有某一特征的类型,对模板参数进行特例化 混合这两种情况的例子如下 总结: template后<>的列表中要列出没有明确指明类型的 typename 下面是一段示例代码: 下面是另一段 阅读全文
posted @ 2018-12-23 17:17 super行者
摘要:下面代码展示通过继承方式把模板中与参数无关的代码分离出来 阅读全文
posted @ 2018-12-23 10:44 super行者
摘要:#include using namespace std; class Shape { public: virtual void display(void) = 0; virtual void setValue(void) = 0; }; class Circle: public Shape { public: Circle(int x=1):radius(x){... 阅读全文
posted @ 2018-12-20 22:02 super行者
摘要:代码中Author和Singer以virtual方式继承Person,解决了Author_Singer实例拥有两份Person copy的问题,这是一种解决方法。 另一种方法是明确指定引用的子类:pp.Author::eat(); 下面代码显示了,使用和不使用virtual来继承基类时,各个类的构造 阅读全文
posted @ 2018-12-19 21:40 super行者
摘要:1.如何在预处理阶段计算sizeof? 阅读全文
posted @ 2018-12-18 06:29 super行者
摘要:#include #include using namespace std; class Person { public: virtual void print(void) { cout << "I am a Person!" << endl; } }; class Chinese: public Person { public: virt... 阅读全文
posted @ 2018-12-18 05:26 super行者
摘要:下面代码显示了不同调用虚函数时的实际执行情况: 执行结果为: 阅读全文
posted @ 2018-12-18 05:17 super行者
摘要:#include #include using namespace std; class String { public: String(const char * x=NULL); String(const String & x); ~String(); String & operator= (const String & x); Stri... 阅读全文
posted @ 2018-12-15 13:43 super行者
摘要:下面代码侧面说明,编译器在构造函数末尾处初始化vptr。所以如果在构造函数中调用virtual函数,动态绑定不会起作用,只会调用本类中的virtual函数: 下面代码是我自己写的读取虚函数表示例: 屏幕输出: 如果将 改为 class Base{ public: virtual void func0 阅读全文
posted @ 2018-12-13 20:45 super行者
摘要:#include #include using namespace std; class MyString { public: MyString(const char * s=NULL) { if (s == NULL) { m_string = new char[1]; m_string[... 阅读全文
posted @ 2018-12-13 06:24 super行者