c++里用new动态分配的结构体对象如何进行初始化

如题所述

第1个回答  2018-09-14
A* a = new A();这是初始化
int* a = new int(123);初始化a并将a赋值为123,取值用*a;
第2个回答  2018-09-14
#include <string>
#include <iostream>
struct A
{
int a;
std::string s;
char c;
};

int main()
{
A a{5,"test",'c'};
std::cout << a.a << "," << a.s << "," << a.c << std::endl;
A *b = new A({ 15,"test123",'d' });
std::cout << b->a << "," << b->s << "," << b->c << std::endl;
getchar();
delete b;
return 0;
}本回答被网友采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网