一 虚指针,虚表
动态绑定 1) 指针 2)向上转型 3)虚函数
(* p->vptr[n])p /(* (p->vptr)[n])p
多态: 通过一个父类的指针容器, 完成子类展示和遍历
二 this
Template Method
(*(this->vptr)[n])(this)
三 动态绑定
一个参考链接: http://blog.csdn.net/haoel/article/details/1948051
模板技术,RTTI技术,虚函数技术
四 const
const ,non-const 同时存在 const object 只会调用const版本, non-const只会调用 non-const
non-const 调用 non-const 需要进行copy on write
#ifndef __TEST__#define __TEST__#include#includeusing namespace std;
typedef void (*func)(void);
class Fruit{
int no;
double weight;
char key;
public:
void print() { cout << "print non-const" << endl;}
void print() const { cout << "print const" << endl; }
};
#endif
test.cpp
#include#include "test.h"
using namespace std;
int main() {
const Fruit *c = new Fruit;
Fruit *d = new Fruit;
c->print();
d->print();
}
output:
print const const Fruit *c调用了 void print() const
print non-const Fruit *d 调用了 void print()
四 new delete 及重载
new先分配memory 在ctor
delete 先dtor再 释放memory
重载时需要要注意是否时修改全局new/delete 还是member operator new/delete or new[] /delete[]
placement new
operate new 第一个参数需要时size_t