c++顶层父类的创建

    技术2023-10-04  76

    #include "Object.h" #include<cstdlib> #include <iostream> using namespace std; namespace DTLib{ //throw() 当前函数不会抛出异常说明 void* Object::operator new(size_t size)throw(){ //cout<<size<<endl; return malloc(size); } void Object::operator delete(void* p){ //cout<<p<<endl; free(p); } void* Object::operator new[](size_t size)throw(){ return malloc(size); } void Object::operator delete[](void* p){ free(p); } Object::~Object(){ } } #include <iostream> #include"exception.h" #include "Object.h" using namespace std; using namespace DTLib; class Test :public Object{ public: int i; int j; }; class Child :public Test { public: int k; }; int main() { Object *obj1= new Test(); Object *obj2= new Child(); cout<<obj1<<endl; cout<<obj2<<endl; delete obj1; delete obj2; return 0; }

    最后的结果 和老师说的有不一样,还要思考 64位电脑 int 占8位字节;

    Processed: 0.023, SQL: 9