【发布时间】:2011-06-12 19:52:39
【问题描述】:
我正在尝试将程序转换为 OOP。该程序适用于几个数组:
int tipoBilletes[9] = { 500,300,200,100,50,20,10,1,2 };
int cantBilletes[9] = {0};
所以对于我的转换,我在头文件中声明:
int *tipoBilletes;
int *cantBilletes;
在我写的构造函数中
tipoBilletes = new int[9];
cantBilletes = new int[9];
tipoBilletes[0] = 500;
tipoBilletes[1] = 300;
tipoBilletes[2] = 200;
...
效果很好。
我的问题是,有没有办法像在 Java 中一样初始化它?
int[] tipoBilletes = new int[]{ 500,300 };
而不是必须一个一个地设置每个元素?
【问题讨论】:
-
直到新版本的 C++ 出来。但是你应该使用
std::vector,而不是new[]。另外,将它从固定大小的数组更改为动态数组有什么好处? -
我仍然无法理解如何在没有旧 C++ 中的默认构造函数的情况下拥有像普通的本地对象数组这样简单的东西......他们有吗,比如,做C++03的时候忘记了,还是什么?
-
好吧,我使用了动态数组,因为我认为我可以像 java 一样进行初始化
-
@Kos:是什么让你觉得不可能?
-
@BoundaryImposition 抱歉,我真的不记得我的意思了,已经有一段时间了。
标签: c++ arrays oop constructor initialization