c++ struct string malloc

    xiaoxiao2026-04-18  0

    在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";//正确

    转载请注明原文地址: https://ju.6miu.com/read-1308976.html
    最新回复(0)