铺垫:C++类和对象的原理
Class = data structure + code (methods).
Object = memory allocation + data + virtual functions.
背景
众所周知,C++实例化对象有两种方式
class simple_class {
private:
int val;
public:
simple_class(int val);
int getVal() const;
};
直接调用构造函数
void test_constructor() {
simple_class sp(0xFFFF);
std::cout << "val: " << sp.getVal() << std::endl;
}
通过operate new去申请空间,赋值给一个类指针
void test_ptr() {
simple_class *spp;
spp = new simple_class(0xAAAA);
std::cout << "val: " << spp->getVal() << std::endl;
}
两种方法的汇编也会有所差距