它有效,但它是对的
It works, but is it right?
本文关键字:有效 更新时间:2023-10-16
我是否借助对象动态数组中的方法填充对象字段。它有效,但它是对的吗?
EXAM* Array = new EXAM[n];
for (int i=0;i<n;i++)
{
cout<<endl;
cout<<"Please type a name of the student: ";
cin>>naMe;
Array[i].setName(naMe);//Is it OK to access an object method this way in case of dynamic allocation?
cout<<endl<<"Please type the date of exam: ";
cin>>daTe;
Array[i].setDate(daTe);
cout<<"Please type mark: ";
cin>>maRk;
Array[i].setMark(maRk);
}
是的
,以下结构是等效的:
pointer[i].method();
(pointer + i)->method();
(*(pointer + i )).method();
类似这个:
object.method();
(&object)->method();
(*(&object)).method();
那只是用不同的语法来表达同样的事情。指针来自并不重要,即它是否指向动态分配的内存,本地或全局对象。使用指针和数组的语法保持不变。
是什么让你觉得不对?
如果标记和日期也是字符串(或在 setDate 和 setMark 函数中转换(,这看起来像是好的代码!
相关文章:
- 欧拉项目#8答案是大以获得有效答案
- 调整大小后指向元素值的指针unordered_map有效?
- 为什么是0;C++中的有效语句
- 最高有效数字侧的第N位
- GCC对可能有效的代码抛出init list生存期警告
- 有效地使用std::unordered_map来插入或增加键的值
- c++中O(n^(1/3))中一个数的除数的有效计数
- 使用无符号字符数组有效存储内存
- 自定义先决条件对移动分配运算符有效吗
- 为什么将值返回函数传递给重载=运算符对运算符函数有效,而对其他运算符无效
- 有哪些有效的方法可以消除一组 100 万个字符串>重复数据?
- 为什么这种直接初始化有效?(C++17)
- 递归函数有效,但无法记忆
- 在C++中初始化向量映射的最有效方法
- 如果变量名称不跟在 char* 后面,const char* 是否有效?
- 钳制迭代器是否有效
- 如何有效地在 std::vector 中插入一对?
- C++模板 t 不是有效的模板类型
- 检查由括号、方括号和大括号组成的一组方括号是否有效?
- 将此布尔值传递给此函数的最有效方法是什么?