C++ では、より効率的な記述方法はありますか。
ImageButton* imageButton;
ImageButton* imageButton1;
ImageButton* imageButton2;
ImageButton* imageButton3;
すべての行を書き出さずになど?私は1000以上のボタンを持っています。これを書くためのより良い方法があることを願っていました。
ありがとう
C++ では、より効率的な記述方法はありますか。
ImageButton* imageButton;
ImageButton* imageButton1;
ImageButton* imageButton2;
ImageButton* imageButton3;
すべての行を書き出さずになど?私は1000以上のボタンを持っています。これを書くためのより良い方法があることを願っていました。
ありがとう
多くの変数を使用することを主張する場合は、このように 1 行で実行します。
ImageButton *imageButton, *imageButton1, *imageButton2 ;
ある方法で星を消すこともできますが、それでもこの方法はあなたの方法と同じかそれ以上です。オブジェクトの配列を使用する方が良いでしょう。
ImageButton [] ;
または後で成長させたい場合は動的なもの。
ImageButton * imagebutton = new ImageButton [size] ;
次のようにする必要があります。
ImageButton *imageButton, *imageButton1, *imageButton2, *imageButton3;
つまり、*
変数ごとに個別に存在する必要があります。または、次のようにすることもできます。
typedef ImageButton* ImageButtonPtr ;
ImageButtonPtr imageButton, imageButton1, imageButton2, imageButton3;
nButtons = 1000
std::vector<ImageButton> images;
images.assign(nButtons, ImageButton())
これは何ですか?:
#include<iostream>
using namespace std;
class ImageButton
{
};
int main()
{
int i;
int BUTTONSIZE=32*32; // 32x32 pixels
int BUTTONAMOUNT=1000; // 1000 buttons
ImageButton **imagebutton = 0;
//memory allocated for 1000 elements , BUTTONAMOUNT=1000
imagebutton = new ImageButton *[BUTTONAMOUNT] ;
//memory allocated for elements of each button.
for( i = 0 ; i < BUTTONAMOUNT ; i++ )
{
imagebutton[i] = new ImageButton[BUTTONSIZE];
cout<<i<<". \n";
}
//free the allocated memory
for( i = 0 ; i < BUTTONAMOUNT ; i++ ) delete [] imagebutton[i] ;
delete [] imagebutton ;
return 0;
}