结构体里的string 要用new分配内存 。malloc只是分配内存。 new除了分配内存还会调用构造函数的

来源:互联网 发布:武汉seo大牛 编辑:程序博客网 时间:2024/05/15 21:53
<span style="font-size:24px;">#include<string> using namespace std;#include <iostream>;#include<stdio.h>struct temp{string s;};void main(){const string p="aaa";temp *q;//q=(struct temp*)malloc(sizeof(struct temp));        q = new temp;q->s=p;  //printf("%s",q->s);    cout<<q->s;}</span>

0 0
原创粉丝点击