Shaul
Shaul

Reputation: 469

const array initialization in C++ with no initial size

I have a method in a parent class that does this:

HWND SppTab::CreateToolTip(HWND hDlg)
{
    LRESULT  added, active;
    const int Controls[] = {
        /* Audio Tab (IDD_AUDIO ) */
        IDC_AUD_AUTO, IDC_AUD_8, IDC_AUD_16, IDC_CH_AUTO, IDC_LEFT, IDC_RIGHT, IDC_LEVEL_L, IDC_LEVEL_R, IDC_LEVEL_M,

        /* Transmitter Tab (IDD_DECODE) */
        IDC_DEC_AUTO, IDC_BTN_SCAN, IDC_LIST_PPM, IDC_LIST_PCM, 
        IDC_CH1, IDC_CH2, IDC_CH3, IDC_CH4, IDC_CH5, IDC_CH6, IDC_CH7, IDC_CH8, IDC_CH9, IDC_CH10, IDC_CH11, IDC_CH12, IDC_CH13, IDC_CH14, IDC_CH15, IDC_CH16
    };

       // Loop on all controls that require tooltip
       for (auto ctrl : Controls)
       {
           HWND hwndTool = GetDlgItem(hDlg, ctrl);
           toolInfo.uId = (UINT_PTR)hwndTool;
           added = SendMessage(m_hwndToolTip, TTM_ADDTOOL, 0, (LPARAM)&toolInfo);
           active = SendMessage(m_hwndToolTip, TTM_ACTIVATE, TRUE, (LPARAM)&toolInfo);
       };
   }

   return m_hwndToolTip;
}

The only difference between the derived methods is the content of array Controls[]. Seems like the array should be a member of the derived classes, and will be initialized differently. Note that the array size is not fixed.

How do I initialize an array which is a class member? I know I can define global arrays (for each class) and assign them in the constructors. Is this that best way to go?

+++++++++++++++++++++++ EDIT ++++++++++++++++++++++++++

Well, you can always define a global array const int g_RawBarId[] = {IDC_CH1,IDC_CH2,IDC_CH3,IDC_CH4,IDC_CH5,IDC_CH6,IDC_CH7,IDC_CH8,\ IDC_CH9,IDC_CH10,IDC_CH11,IDC_CH12,IDC_CH13,IDC_CH14,IDC_CH15,IDC_CH16}; and then assign it to a vector in the constructor: m_vRawBarId(g_RawBarId, g_RawBarId+sizeof(g_RawBarId)/ sizeof(int)) However, using globals feels like breaking the rules of the OO game. If you have a better idea - an example will be invaluable.

Upvotes: 0

Views: 116

Answers (2)

Since your array appears to be constant data that is only there to control what CreateToolTip() does, the most prudent approach would be to just define the two (or more) arrays as static constants and to use a pointer to one or the other in CreateToolTip().

That way you avoid unnecessary copying of data, unnecessary array initialization, unnecessary memory allocations, and unnecessary pains trying to deal with variable array sizes.

Upvotes: 2

user2734982
user2734982

Reputation:

Initialization of this member array within class constructors is probably the right one. There is no best way I suppose. All will ultimately give same results if you do it properly.

If you are using C++11 you can initialize members in class definition itself. But if using dynamic memory then I will recommend giving bare minimum memory first in constructor and then increase as needed.

Also, the array size is unknown so you can use dynamic member array allocation.

It is done using malloc and calloc in C and new in C++.

Or, you can use some data type which manages dynamic memory allocation itself whose example is std::vector in C++.

Upvotes: 0

Related Questions