您的位置:首页 > 其它

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

2015-04-02 16:09 218 查看
<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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: