C++动态结构数组如何简单初始化?
我的代码是这样子的:#include<iostream>#include<cstring>structCandyBar{charband[20];floatweight;...
我的代码是这样子的:
#include <iostream>
#include <cstring>
struct CandyBar
{
char band[20];
float weight;
int cal;
};
int main()
{
using namespace std;
CandyBar *type;
type = new CandyBar[3];
// *type = {"Coca Cola", 23.11, 1200};
// *(type + 1) = {"Coca Cola", 23.11, 1200};
// *(type + 2) = {"Coca Cola", 23.11, 1200};
strcat(type->band, "Coca Cola");
type->weight = 23.11;
type->cal = 1200;
strcat((type + 1)->band, "Nestle");
(type + 1)->weight = 11.11;
(type + 1)->cal = 1500;
strcat((type + 2)->band, "Pepsico");
(type + 2)->weight = 3.11;
(type + 2)->cal = 600;
for (int i=0; i <=2; i++) {
cout << "\nCandyBar " << i << ":\n";
cout << "Band: " << (*(type + i)).band << endl;
cout << "Weight: " << (*(type + i)).weight << endl;
cout << "Pepsico: " << (*(type + i)).cal << endl;
}
return 0;
}
感觉初始化的方法好笨,我希望能跟静态申明一样可以直接赋值,或者跟我注释的语句一样赋值。有没有什么办法? 展开
#include <iostream>
#include <cstring>
struct CandyBar
{
char band[20];
float weight;
int cal;
};
int main()
{
using namespace std;
CandyBar *type;
type = new CandyBar[3];
// *type = {"Coca Cola", 23.11, 1200};
// *(type + 1) = {"Coca Cola", 23.11, 1200};
// *(type + 2) = {"Coca Cola", 23.11, 1200};
strcat(type->band, "Coca Cola");
type->weight = 23.11;
type->cal = 1200;
strcat((type + 1)->band, "Nestle");
(type + 1)->weight = 11.11;
(type + 1)->cal = 1500;
strcat((type + 2)->band, "Pepsico");
(type + 2)->weight = 3.11;
(type + 2)->cal = 600;
for (int i=0; i <=2; i++) {
cout << "\nCandyBar " << i << ":\n";
cout << "Band: " << (*(type + i)).band << endl;
cout << "Weight: " << (*(type + i)).weight << endl;
cout << "Pepsico: " << (*(type + i)).cal << endl;
}
return 0;
}
感觉初始化的方法好笨,我希望能跟静态申明一样可以直接赋值,或者跟我注释的语句一样赋值。有没有什么办法? 展开
4个回答
展开全部
写函数!
完整代码:
#include <iostream>
#include <cstring>
using namespace std;
struct CandyBar
{
char band[20];
float weight;
int cal;
void init(char* str,float w,int c)
{
memset(band,0,20);
strcat(band,str);
weight=w;
cal=c;
}
void mprint()
{
cout<<"Band: " <<band
<<"\nWeight: " <<weight
<<"\nPepsico: " <<cal<<endl;
}
};
int main()
{
CandyBar *type;
type = new CandyBar[3];
type[0].init("Coca Cola", 23.11, 1200);
(*(type+1)).init("Nestle", 11.11, 1500);//哥写成这样子只是为了说明type[1]和(*(type+1))是一回事。
type[2].init("Pepsico", 3.11, 600);
for (int i=0; i <=2; i++)
{
cout << "\nCandyBar " << i << ":\n";
type[i].mprint();
}
return 0;
}
完整代码:
#include <iostream>
#include <cstring>
using namespace std;
struct CandyBar
{
char band[20];
float weight;
int cal;
void init(char* str,float w,int c)
{
memset(band,0,20);
strcat(band,str);
weight=w;
cal=c;
}
void mprint()
{
cout<<"Band: " <<band
<<"\nWeight: " <<weight
<<"\nPepsico: " <<cal<<endl;
}
};
int main()
{
CandyBar *type;
type = new CandyBar[3];
type[0].init("Coca Cola", 23.11, 1200);
(*(type+1)).init("Nestle", 11.11, 1500);//哥写成这样子只是为了说明type[1]和(*(type+1))是一回事。
type[2].init("Pepsico", 3.11, 600);
for (int i=0; i <=2; i++)
{
cout << "\nCandyBar " << i << ":\n";
type[i].mprint();
}
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
楼上的办法不行.
只能初始化单个堆中对象,人家是对象数组.还有构造函数体中应该是strcpy()不是strcat()
我在研究中想办法...
对象数组不在堆中可以这么搞,在堆中不行.
#include <iostream>
using namespace std;
struct CandyBar
{
CandyBar(char *ch,float fl,int i):weight(fl),cal(i){strcpy(band,ch);}
char band[20];
float weight;
int cal;
};
int main ()
{
CandyBar type[3]={CandyBar("Coca",23.11,1200),CandyBar("Coca",23.11,1200),CandyBar("Coca",23.11,1200)};
return 0;
}
只能初始化单个堆中对象,人家是对象数组.还有构造函数体中应该是strcpy()不是strcat()
我在研究中想办法...
对象数组不在堆中可以这么搞,在堆中不行.
#include <iostream>
using namespace std;
struct CandyBar
{
CandyBar(char *ch,float fl,int i):weight(fl),cal(i){strcpy(band,ch);}
char band[20];
float weight;
int cal;
};
int main ()
{
CandyBar type[3]={CandyBar("Coca",23.11,1200),CandyBar("Coca",23.11,1200),CandyBar("Coca",23.11,1200)};
return 0;
}
追问
额,可以解释下CandyBar(char *ch,float fl,int i):weight(fl),cal(i){strcpy(band,ch);}这句话吗?好像跟一般的语句不一样。而且我用gcc编译会有些警告:
test1.cpp: In function ‘int main()’:
test1.cpp:13:47: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
...
你知道是什么原因吗?谢谢了!
追答
CandyBar(char *ch,float fl,int i):weight(fl),cal(i){strcpy(band,ch);}
意思就是说,在结构体一个对象初始化时必须给定三个参数.像CandyBar ob(参数,参数,参数)这样才行.或是CandyBar *p =new CandyBar(参数.参数.参数)
在建立一个对象时, CandyBar(char *ch,float fl,int i):weight(fl),cal(i){strcpy(band,ch);}这个函数会被调用.weight(fl),cal(i)就是说将小括号中的值赋给结构体的成员.你也可以全部写在大括号中像这样{weight=fl;cai=i;}
至于警告是因为字符串转换上的原因.你把strcpy()函数换成memcpy()试下
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
简单例子
#include <iostream>
#include <cstring>
struct CandyBar
{
char band[20];
float weight;
int cal;
CandyBar(const char *tband,float tweigth,int tcal)
{
strcat(band, tband);
weight=tweigth;
cal=tcal;
}
};
int main()
{
using namespace std;
CandyBar *a= new CandyBar("dfada",3,4) ;
cout<<a->band<<endl;
cin.get();
}
#include <iostream>
#include <cstring>
struct CandyBar
{
char band[20];
float weight;
int cal;
CandyBar(const char *tband,float tweigth,int tcal)
{
strcat(band, tband);
weight=tweigth;
cal=tcal;
}
};
int main()
{
using namespace std;
CandyBar *a= new CandyBar("dfada",3,4) ;
cout<<a->band<<endl;
cin.get();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
C++中struct和类一样,可以有构造函数的。给CandyBar写个构造函数就行了。
追问
可以给个简单的例子吗?我初学C++,还没有看到类和构造函数那个地方。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询