随笔分类 -  语言C++

摘要:先调用基类构造函数,再调用派生类构造函数 123456789101112131415161718 classBase{public: Base() { cout << "Base()" << endl;}};classDerived : publicBase{public: Derived() { cout << "Derived()" << endl;}};intmain(){ Der... 阅读全文
posted @ 2013-07-23 16:04 helloweworld 阅读(1054) 评论(0) 推荐(0)
摘要:EFfective C++和More Effective C++中有讲解。“条款23: 必须返回一个对象时不要试图返回一个引用”“条款31: 千万不要返回局部对象的引用,也不要返回函数内部用new初始化的指针的引用”为什么要返回引用呢——为了实现链式操作。返回一个对象不行吗?为什么有时要返回引用呢?主要是为了实现链式操作。看一个例子就明白了:1234567891011121314151617181920MyString& MyString::operator =(constMyString &other) { /*if (this != &other) { size = 阅读全文
posted @ 2013-07-23 15:19 helloweworld 阅读(650) 评论(0) 推荐(0)
摘要:http://www.cppblog.com/mydriverc/articles/33991.html 12345678910111213 intmain(){ string str = "0101"; bitset b1(str); bitset b2(5); cout << b2[3] << endl; cout << b2[2] << endl; cout << b2[1] <... 阅读全文
posted @ 2013-07-16 18:15 helloweworld 阅读(246) 评论(0) 推荐(0)
摘要:#includeusingnamespacestd;classTest{public: intstra; staticintsta;//静态数据成员并不会介入对象内存布局。 intstrb; intstrc;};intTest::sta=1;intmain(){ inta; intb; intc; Tests; cout *freeList; private: float y; static co... 阅读全文
posted @ 2013-07-05 17:20 helloweworld 阅读(442) 评论(0) 推荐(0)
摘要:除static const修饰的数据成员可在定义时初始化外,其他数据成员不可以在定义时初始化,一般放到成员初始化列表或成员函数中进行初始化。 如下面编译出错: class B{ int a = 1;public:}; 类中成员数据也不能用extern auto register限定其存储类型。 因为类只是一种自定义数据类型,编译时并不给分配存储空间,也不给初始化。 下面编译仍出错: class ... 阅读全文
posted @ 2013-06-04 22:28 helloweworld 阅读(467) 评论(0) 推荐(0)
摘要:关于friend,记住一点,friend函数不是类的成员函数,不含this指针,不能直接访问类的成员,要通过对象访问。 阅读全文
posted @ 2013-06-04 22:20 helloweworld 阅读(138) 评论(0) 推荐(0)
摘要:关于const对象和成员函数,掌握2点:1.const对象只能调用const成员函数,不能调用非const成员函数。(这句话针对的是成员函数,当然也可调用公有数据成员)。2.const成员函数不允许修改数据成员。为什么需要const,仅从类和对象的角度看,如有一个日期类,我们希望定义一个人的生日,该生日不能被修改,怎么办?const Date birthday(1987,11,15);即我们需要定义一个常对象,不允许修改该对象的数据成员,const满足了我们的需求。如何保证常对象的数据成员不被修改呢,我们从修改数据成员的2种方式入手。一是通过对象名调用公有数据成员修改,因为birthday已经 阅读全文
posted @ 2013-06-04 22:18 helloweworld 阅读(205) 评论(0) 推荐(0)
摘要:为什么需要static?比如一个学生类,班级总人数对每个学生对象而言应该是公共的变量。即类的设计需要一个存储公共信息的数据成员,因此static应运而生! 掌握2个知识点: 1.static成员在使用前必须进行定义性说明。 2.静态成员函数不能直接访问类的非静态数据成员,因为静态成员不含this指针。 1.static成员在使用前必须进行定义性说明。 定义性说明:<类型> <类名>::<静态... 阅读全文
posted @ 2013-06-04 22:04 helloweworld 阅读(316) 评论(0) 推荐(0)
摘要:An alternative to the Handle class approach is to make Person a special kind of abstract base class called a Protocol class. Bydefinition, a Protocol class has no implementation; its raison d'être is ... 阅读全文
posted @ 2013-05-29 22:33 helloweworld 阅读(303) 评论(0) 推荐(0)
摘要: 阅读全文
posted @ 2013-05-10 17:24 helloweworld 阅读(117) 评论(0) 推荐(0)
摘要:#include <iostream>using namespace std;class A{ int a; virtual void aa() {}};class B : virtual public A{ int b;};class C : virtual public A{ int c;};class D : public B, public C{ int d;};int main(){ c... 阅读全文
posted @ 2013-05-10 17:10 helloweworld 阅读(144) 评论(0) 推荐(0)
摘要:普通类的sizeof 类比struct 注意字节对齐#include <iostream>using namespace std;class A{public: int data; char p;};int main(){ cout << sizeof (A) << endl; //字节对齐,输出8. return 0;}含有虚函数的类的sizeof#include <iostream>u... 阅读全文
posted @ 2013-05-09 21:42 helloweworld 阅读(240) 评论(0) 推荐(0)
摘要://============================================================================// Name : HelloWorldcpp.cpp// Author : Lucas// Version :// Copyright : @Lucas// Description : Hello Worl... 阅读全文
posted @ 2013-05-08 12:36 helloweworld 阅读(190) 评论(0) 推荐(0)
摘要:是什么 一个类模板,解决delete发生之前发生异常从而导致内存泄露的问题。 使用时需要包含memory头文件。 void f(){ int *ip = new int(42); ...//如果这里发生异常,则下面的delete可能不会执行,导致内存泄露。 delete ip;}#include <iostream>#include <memory>using namespace std;vo... 阅读全文
posted @ 2013-05-07 10:08 helloweworld 阅读(182) 评论(0) 推荐(0)
摘要:为什么需要智能指针? 智能指针出现的背景是什么? 为了解决类中包含指针时的可能会导致的内存泄漏问题。 智能指针是如何实现的? 通过引用计数实现,引用计数的实现有两种:一辅助类,二句柄类。 阅读全文
posted @ 2013-05-06 16:01 helloweworld 阅读(141) 评论(0) 推荐(0)
摘要:重载overload 函数名相同,但形参的个数或形参的类型不同。 仅返回值不同,不能定义为重载函数。 class Base {public: void baseFun(int) { cout << "baseFun(int) in Base" << endl; } void baseFun(double) { cout << "baseFun(float) in Base" << endl; }}... 阅读全文
posted @ 2013-04-25 22:22 helloweworld 阅读(171) 评论(0) 推荐(0)
摘要:类成员的访问权限 首先来看类成员的访问权限。 private: 只能由该类中的函数、其友元函数访问,不能被任何其他访问,该类的对象也不能访问. protected: 可以被该类中的函数、子类的函数、以及其友元函数访问,但不能被该类的对象访问 public: 可以被该类中的函数、子类的函数、其友元函数访问,也可以由该类的对象访问。 记忆:类内函数/友元函数、子类函数、对象。 我们经常听到这样的说法... 阅读全文
posted @ 2013-04-23 22:01 helloweworld 阅读(747) 评论(0) 推荐(0)
摘要:static_cast、dynamic_cast、const_cast、reinterpret_cast 用法:cast-name<type>(expression); 如,double d = 3.14; char c = static_cast<char>(d); static_cast(主要用于代替C) 用法:static_cast < type-id > ( expression ) 该... 阅读全文
posted @ 2013-04-21 10:58 helloweworld 阅读(227) 评论(0) 推荐(0)
摘要:一、一个例子 #include <iostream>using namespace std;int main(){ try { if (1) { throw exception(); //catch中用引用。// new throw exception(); //catch 中用指针。 } } catch (exception &e) { cerr << "catch except... 阅读全文
posted @ 2013-04-19 18:12 helloweworld 阅读(124) 评论(0) 推荐(0)
摘要:http://www.cnblogs.com/zhuyp1015/archive/2012/07/25/2609129.html C++ 程序员都应该知道,只有在程序通过指向基类对象的指针或基类对象的引用调用虚函数时,才会发生运行时的多态现象。 对象的创建和复制不是运行时多态的,这一点严重影响了类的设计。所以,容器——无论是类似于数组或者结构体的内建容器还是用户自定义容器类——只能获得编译时类型一致的元素值。如果有一系列类之间存在继承关系,当我们需要创建、复制和存储对象,而这些对象的确切类型只有到运行时才能知道时,则这种编译时的检查会带来一些麻烦。 通常,解决这个问题的方法是增加一个间... 阅读全文
posted @ 2013-01-14 21:29 helloweworld 阅读(319) 评论(0) 推荐(0)