C++ 我知道数组长度,但想知道是否是一种更简洁的数组元素定义方法
C++ I know the array length but was wondering if a cleaner way of defining the array elements
>我有以下内容:
Action* actions[];
在操作栏类中。
我想在其构造函数中做这样的事情:
actions = {
new Action( new Image( gfx, "Images/ActionBar/Push001.png", 200, 200, TRUE ), 0, 200, 200, 200, 200 ),
new Action( new Image( gfx, "Images/ActionBar/Pull001.png", 200, 200, TRUE ), 1, 200, 200, 200, 200 )
};
最初我在做:
Action* actions[ 2 ];
然后在构造函数中:
actions[ 0 ] = new Action( new Image( gfx, "Images.....
actions[ 1 ] = new Action( new Image( gfx, "Images.....
最好的方法是什么?这样最后我就可以在我的游戏循环中做一些类似的事情
SomeFunctionIPassAnActionInto( actionBar->actions[ 0 ] );
编辑::稍微改变了问题,我总是知道会有5个动作,所以如果我这样做了
Actions* actions [ 5 ];
我将如何像这样声明数组元素:
actions = {
new Action( "push" ),
new Action( "pull" ),
new Action( "bla" ),
new Action( "ble" ),
new Action( "blo" )
}
那种事情
在 C++11 中,您可以在 ctor 初始值设定项中初始化数组。
ActionBar::ActionBar()
: actions {
new Action( new Image( gfx, "Images/ActionBar/Push001.png", 200, 200, TRUE ), 0, 200, 200, 200, 200 ),
new Action( new Image( gfx, "Images/ActionBar/Pull001.png", 200, 200, TRUE ), 1, 200, 200, 200, 200 )
}
{
}
How would I declare the array elements like this:
actions = {
new Action( "push" ),
new Action( "pull" ),
new Action( "bla" ),
new Action( "ble" ),
new Action( "blo" )
}
使其工作的最简单方法是为 Action
定义一个构造函数,该构造函数采用 char const*
、 char const* const*
或 std::string const&
中的一个,然后将此参数(可能使用您展示的示例的某种翻译(转发到 Image 构造函数(由于 image 是成员,您需要在初始值设定项列表中执行一些操作,例如 _image(new Image(translate(arg)))
其中translate
是您定义的函数,用于转动 push
成Images/ActionBar/Push001.png
(。
相关文章:
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 有没有一种方法可以在编译时获得作用域类名
- 对于C++中使用智能指针的指针算术限制,有没有一种变通方法
- 一种在C++中读取TXT配置文件的简单方法
- 有没有一种方法可以测量c++程序的运行时内存使用情况
- 有没有一种方法可以使用placement new将堆叠对象分配给分配的内存
- 在调用接收数组的方法时,模板化数组大小是不是一种糟糕的做法
- 有没有一种方法可以通过"typedef"为重新定义的基本类型定义特征和强制转换运算符
- 有没有一种"cleaner"的方法可以在指向基的指针向量中找到派生类的第一个实例?
- 有没有一种代码密度较低的方法来使用非默认构造函数初始化数组?
- 将错误返回给调用方而不是立即在 C++ 中抛出错误是否是一种好的做法
- 在 c++ 中,有一种方法可以创建一个包含地图作为值的树状地图?
- 有没有一种优雅而快速的方法来测试整数中的 1 位是否位于连续区域
- 在运行时检查继承是否只有一种类型和 void*
- C++ STD 函数运算符:有没有一种方法可以通过函数将一个向量映射到另一个向量上?
- 是否有一种简洁的方法可以将文本输出到调试窗格
- 在构造过程中是否有一种简洁的方法可以派生成员的类型?
- C++ 我知道数组长度,但想知道是否是一种更简洁的数组元素定义方法