0

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.

2
  • 1
    Since the size is not known, std::vector. Commented Jun 11, 2014 at 14:47
  • are you writing C++11? then this is fairly simple to solve. Commented Jun 11, 2014 at 14:50

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't think that using a static array is breaking the rules of the OO game. The point is encapsulation, not lifetime of storage. If you use a static array which is well encapsulated within a single source file, there's nothing wrong with that. Another argument is constness: You don't want to use 3.14159 everywhere for fear of defining and using a global PI, do you? If it's constant, there is no use in limiting its scope. It can't change so it does not create any intransparent dependencies between parts of the code. Your array is both encapsulated and constant, so where's the problem?
I thought I knew C++ fairly well but have just learnt something new. static keyword for global variable has a scope limiting effect.
0

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++.

1 Comment

You still need to define the array somewhere. Assuming I declare a pointer in the class declaration: const int * m_array; then in the class constructor I need to initialize the array somehow. Is there a correct syntax to do this: m_array = new const int{1,1};? Not that I know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.