在结构体里,什么时候用-> 什么时候用.
#include<iostream>#include<string>usingnamespacestd;structCat{intweight;stringname;};...
#include <iostream>
#include <string>
using namespace std;
struct Cat
{
int weight;
string name;
};
typedef Cat CAT;
int main()
{
CAT c1;
c1.weight = 5;
c1.name = "jerry";
cout << c1.name << " " << c1.weight << endl;
Cat c2;
c2.weight = 3;
c2.name = "tom";
cout << c2.name << " " << c2.weight << endl;
CAT* pc = new Cat();
pc->weight = 10;
pc->name = "kitty";
cout << pc->name << " " << pc->weight << endl;
delete pc;
CAT* pc1 = &c1;
cout << pc1->name << " " << pc1->weight << endl;
CAT* pcc2 = new Cat[2];
pcc2[0] = c1;
pcc2[1] = c2;
cout << pcc2[0].name << " " << pcc2[0].weight << endl;
cout << pcc2[1].name << " " << pcc2[1].weight << endl;
delete[] pcc2;
return 0;
}
请问:什么时候用-> 什么时候用. 展开
#include <string>
using namespace std;
struct Cat
{
int weight;
string name;
};
typedef Cat CAT;
int main()
{
CAT c1;
c1.weight = 5;
c1.name = "jerry";
cout << c1.name << " " << c1.weight << endl;
Cat c2;
c2.weight = 3;
c2.name = "tom";
cout << c2.name << " " << c2.weight << endl;
CAT* pc = new Cat();
pc->weight = 10;
pc->name = "kitty";
cout << pc->name << " " << pc->weight << endl;
delete pc;
CAT* pc1 = &c1;
cout << pc1->name << " " << pc1->weight << endl;
CAT* pcc2 = new Cat[2];
pcc2[0] = c1;
pcc2[1] = c2;
cout << pcc2[0].name << " " << pcc2[0].weight << endl;
cout << pcc2[1].name << " " << pcc2[1].weight << endl;
delete[] pcc2;
return 0;
}
请问:什么时候用-> 什么时候用. 展开
1个回答
展开全部
当变量是指针的时候,访问其成员就要用 -> 否则就用.(点)
追问
但上面最后一个是指针 为何不是->
追答
你说的是pcc2[0] 这个是吧
你看你的赋值语句: pcc2[0] = c1; 他和c1是一样的,而c1是Cat对象,所以用点
pcc2是指针,所以pcc2[0].weight 也可以写作 pcc2->weight
pcc2[1].weight可以写作 (pcc2+1)->weight
这一块是指针和数组的关系,你可以去看一下相关的知识
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询