C++
bigsnowstorm
这个作者很懒,什么都没留下…
展开
-
Type Casting
Type CastingConverting an expression of a given type into another type is known as type-casting. We have already seen some ways to type cast:Implicit conversionImplicit conversions do not转载 2013-04-20 10:30:19 · 1027 阅读 · 0 评论 -
Favor composition over inheritance
Composition is also known as HAS-A relation andinheritance is also known as IS-A relation. And HAS-A can be better than an IS-A relationship.So make it a habit of always preferring composition原创 2013-12-07 23:17:16 · 800 阅读 · 0 评论 -
父类指针向子类指针的强制类型转换
结论:定义一个父类对象,将该父类对象的指针强制类型转换为子类指针,并赋给一个子类指针,则:该子类指针将指向该父类对象,但是其this指针仍然是指向子类的指针。当该指针调用虚成员函数时,将按照对象的内存布局(指向虚函数表的指针存储在该对象的前四个字节)调用父类虚成员函数。当该指针调用普通成员函数时,将通过this指针调用位于代码段的该子类的成员函数。 例1、非虚成员函数typ原创 2013-04-14 17:16:31 · 3389 阅读 · 0 评论 -
Virtual Inheritance
C++ primer:Virtual Inheritance:A base class is specified as being derived through virtualinheritance by modifying its declaration with the keyword virtual.个人修正:A base class is specified as bei原创 2013-04-16 17:45:29 · 851 阅读 · 0 评论 -
C/C++ note
突然间想不起来指针的英文单词,pointer。不清楚char *c,c到底是什么?c是一个指针变量,可以存储一个地址,指向的类型是char类型。A pointer is equivalent to the address of the first element that it points to。因此,c也可以看做是一个char []首元素的地址。char **c原创 2013-11-13 22:11:45 · 814 阅读 · 0 评论 -
验证:合成的复制构造函数是位拷贝的
以下摘自林锐的高质量C++编程:如果不主动编写拷贝构造函数和赋值函数,编译器将以“位拷贝”的方式自动生成缺省的函数。倘若类中含有指针变量,那么这两个缺省的函数就隐含了错误。以类String 的两个对象a,b 为例,假设a.m_data 的内容为“hello”,b.m_data 的内容为“world”。现将a 赋给b,缺省赋值函数的“位拷贝”意味着执行b.m_data = a.m_data。原创 2013-10-11 00:47:21 · 765 阅读 · 1 评论 -
String类:构造函数,拷贝构造函数
#include #include using namespace std;class String{public: String(const char * str = NULL); //默认参数只需在函数声明时注明,函数定义时不需要 String(const String &other); String & operator=(const String &other); ~S原创 2013-08-27 08:18:23 · 738 阅读 · 0 评论 -
陈皓:C++ 虚函数表解析----笔记
文章链接:http://blog.csdn.net/haoel/article/details/1948051虚函数表C++中的虚函数的作用主要是实现了多态的机制。关于多态,简而言之就是用父类型别的指针指向其子类的实例,然后通过父类的指针调用实际子类的成员函数。对C++ 了解的人都应该知道虚函数(Virtual Function)是通过一张虚函数表(Virtual Table)来实原创 2013-09-28 17:58:41 · 1136 阅读 · 0 评论 -
陈皓-C++ 对象的内存布局(上):笔记
单一的一般继承代码如下:#include using namespace std;class Parent {public: int iparent; Parent ():iparent (10) {} virtual void f() { cout << " Parent::f()" << endl; } virtual void g() {原创 2013-10-01 09:13:06 · 1198 阅读 · 0 评论 -
default constructor
https://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr376.htmA default constructor is a constructor that either has no parameters,转载 2013-08-19 22:05:57 · 1860 阅读 · 0 评论 -
private, protected, public疑惑
转自:http://blog.codingnow.com/2010/03/cpp_protected.html1、class base{public: base(int i = 1):pri(i) {} int fun(base &b) { return pri + b.pri; }private: int pri;};void fun(base &b){ co转载 2013-09-07 00:01:03 · 637 阅读 · 0 评论 -
private
private:When preceding a list of class members, the private keyword specifies that those members are accessible only from member functions and friends of the class. This applies to all members decla原创 2013-08-29 23:30:09 · 743 阅读 · 0 评论 -
C++ Primer 笔记-- 13 章: 智能指针类
除了使指针成员与指针完全相同之外,另一种方法是定义所谓的智能指针类。智能指针除了增加功能外,其行为像普通指针一样。本例中让智能指针负责删除共享对象。用户将动态分配一个对象并将该对象的地址传给新的 HasPtr类。用户仍然可以通过普通指针访问对象,但绝不能删除指针。HasPtr 类将保证在撤销指向对象的最后一个 HasPtr 对象时删除对象。新的 HasPtr 类需要一个析构函数来删除指针,但是原创 2013-08-26 07:42:45 · 700 阅读 · 1 评论 -
C++ Primer 笔记--13章:复制控制
包含指针的类需要特别注意复制控制,原因是复制指针时只复制指针的地址,而不会复制指针指向的对象。将一个指针复制到另一个指针时,两个指针指向同一个对象,此时可能使用任一指针改变这一对象。类似的,很可能一个指针删除了一对象时,另一指针的用户还认为该对象仍然存在。一个带指针成员的简单类://class that has a pointer member that behave like a p原创 2013-08-25 11:00:30 · 485 阅读 · 0 评论 -
delete delete[]
http://stackoverflow.com/questions/4255598/delete-vs-deleteFrom the standard (5.3.5/2) :In the first alternative (delete object), the value of the operand of delete shall be a pointer to a转载 2013-08-27 07:43:09 · 689 阅读 · 0 评论 -
C++ Primer 笔记--13章:定义值型类
要使指针成员表现得像一个值,复制 HasPtr 对象时必须复制指针所指向的对象。复制构造函数不再复制指针,它将分配一个新的 int 对象,并初始化该对象以保存与被复制对象相同的值。每个对象都保存属于自己的 int 值的不同副本。因为每个对象保存自己的副本,所以析构函数将无条件删除指针。赋值操作符不需要分配新对象,它只是必须记得给其指针所指向的对象赋新值,而不是给指针本身赋值。/* *原创 2013-08-26 23:14:59 · 643 阅读 · 0 评论 -
virtual function
http://en.wikipedia.org/wiki/Virtual_functionThe concept of the virtualfunction solves the following problem:In OOPwhen a derived class inherits from a base class, an object of the derived class转载 2013-08-08 23:26:08 · 498 阅读 · 0 评论 -
C++ primer 笔记:继承与静态成员
如果基类定义了static成员,则整个继承层次中中只有一个这样的成员。无论从基类派生出多少个派生类,每个static成员只有一个。static成员遵循常规访问控制:若果成员在基类中为private,则派生类不能访问它。假定可以访问成员,则既可以通过基类访问static成员,也可以通过派生类访问static成员。验证:#include using namespace std;cl原创 2013-09-06 08:19:53 · 614 阅读 · 0 评论 -
Virtual Destructor
http://www.programmerinterview.com/index.php/c-cplusplus/virtual-destructors/Example without a Virtual Destructor:#include iostream.hclass Base{ public: Base(){ cout<<"Con转载 2013-09-08 17:47:48 · 894 阅读 · 0 评论 -
const
/* * myconst.h * * Created on: 2013-8-10 * Author: Administrator */#ifndef MYCONST_H_#define MYCONST_H_#include #include #include #include using namespace std;原创 2013-08-10 18:35:54 · 466 阅读 · 0 评论 -
C/C++: static
C++中static要点总结:1、静态成员的提出是为了解决数据共享的问题。2、因为静态成员函数是属于类的一部分,而不是某个对象的一部分,因此静态成员函数没有this指针,this指针是用于指向某个对象的。又因为静态成员函数没有this指针,因此在静态成员函数中不能直接调用非静态成员,因为非静态成员都是通过指向对象的this指针隐式或显式调用的。在静态成员函数的实现中不能直接引用类中说明转载 2013-09-26 00:31:40 · 593 阅读 · 0 评论 -
维护代码的架构
维护的代码采用如下架构:class Peripheral{public: Peripheral() {value = 0;} static Peripheral * instance(); void get_value(); void set_value(int new_value);private: int value;};Peripheral * Peripheral原创 2013-11-26 21:44:44 · 592 阅读 · 0 评论