정성훈.해미 님이 쓰신 글 :
: 배열 초기화 처럼 하는 방법은 없으나
: 원하시는 방법이
: 일일이 16번 코딩 안하고
: 한번에 간단하게 매칭시키실려는거 같군요.
:
: 반복문을 써서 코딩하시면 조금 편하실 겁니다.
: FindComponent함수를 이용해서 디자인된 콤포넌트 이름을 찾는 방법을 써보세요.
:
:
:
: TSpeedButton *MOutputBtn[16];
: // ---------------------------------------------------------------------------
: __fastcall TManualFrame::TManualFrame(TComponent* Owner) : TFrame(Owner) {
: for(int i=0; i<16; i++) {
: MOutputBtn[i] = (TSpeedButton*)FindComponent(UnicodeString().sprintf(L"SpeedButton%d", i+1));
: }
: }
:
:
:
:
:
: hss9205 님이 쓰신 글 :
: : TSpeedButton *MOutputBtn[16];
: : // ---------------------------------------------------------------------------
: : __fastcall TManualFrame::TManualFrame(TComponent* Owner) : TFrame(Owner) {
: : MOutputBtn[0] = SpeedButton1;
: : MOutputBtn[1] = SpeedButton2;
: : MOutputBtn[2] = SpeedButton3;
: : MOutputBtn[3] = SpeedButton4;
: : }
: :
: : 대충 이렇게 초기화를 했는대 이 방법 말고 배열을 한번 선언한 뒤에
: : MOutputBtn[] = {speedButton9, SpeedButton10,
: : SpeedButton11, SpeedButton12, SpeedButton13, SpeedButton14,
: : SpeedButton15, SpeedButton16}
: : 과 비슷한 방법으로 하고 싶은대 이개 안되내요
: :
: : 그래서 유사한 방법을 알고 싶습니다.
: :
: : 그리고 TSpeedButton은 벡터로 선언이 안되는 모양인것 같은대 맞나요?
답변:
array를 초기화 하려면 array 선언/정의시에 해야 하는 거지만...
컴파일러가 C++11을 지원하면 vector를 이용해서 루프 이용하지 않고 아래와 같은 식으로 코딩할 수 있어요.
std::vector<TSpeedButton *>arrayButtons;
Foo::Foo()
{
....
arrayButtons.assign({speedBtn1, speedBtn2, speedBtn3}); // C++11, member initialization list
}
간단하죠?
사용할 땐...
arrayButtons[0]->Text = L"Hello";
|