首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

结构体里的string赋值有关问题

2013-12-28 
结构体里的string赋值问题#includestringusing namespace stdstruct temp{string s}int main(){const

结构体里的string赋值问题

#include<string>
using namespace std;
struct temp
{
string s;
};
int main()
{
const string p="aaa";
temp *q;
q=(struct temp*)malloc(sizeof(struct temp));
q->s=p;

}

为何这样赋值会内存报错?
[解决办法]
C++的结构体和类都有默认构造函数的,不写都会自动实现一个。

malloc只是分配内存。

new除了分配内存还会调用构造函数的。

热点排行