c++ struct string malloc

来源:互联网 发布:淘宝试用黑名单恢复 编辑:程序博客网 时间:2024/06/05 04:43

在struct中使用string

正常本地变量是正常的

typedef A {

  string content;

} A;


A a;

a.content = "test";

这个是正常的


但是当使用c的malloc的时候,却出现的错误

A *a = (A*)malloc(sizeof(A));

a->content = "test";//错误

在c++中,应该使用

A *a = new A;

a->content = "test";//正确

0 0