请问怎么样使用malloc函数使得对象的构造函数得以调用?
#include <iostream>#include <cstdlib>#include <cstring>using namespace std;/*我写了下面的代码做了测试,A*ptr=(A*)malloc(sizeof(A));//根本就没有调用构造函数结果表明如此调用malloc函数没有触发构造函数的调用。请问是不是malloc不能够支持C++的面向对象设计?还是我写的函数不对?*/class A{public: A(int _a):a(_a) { cout<<"I am in the init function"<<endl; } A() { cout<<"I am in the default construtor"<<endl; } ~A() { } int get_a() const { return a; }private: int a;};int main(){ A*ptr=(A*)malloc(sizeof(A));//根本就没有调用构造函数 //ptr->A(); 非法表达式 //A*ptr=new A;默认构造函数会被调用,C++特性使然 A* ptr_=new A(12); cout<<"ptr malloc "<<ptr->get_a()<<endl; cout<<"ptr_ new "<<ptr_->get_a()<<endl; free(ptr); delete ptr_; return 0;}