简体   繁体   English

没有初始大小的C ++中的const数组初始化

[英]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[]. 派生方法之间的唯一区别是数组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}; 好吧,您始终可以定义一个全局数组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. 然后将其分配给构造函数中的向量: m_vRawBarId(g_RawBarId, g_RawBarId+sizeof(g_RawBarId)/ sizeof(int))但是,使用全局感觉就像违反了OO游戏的规则。 If you have a better idea - an example will be invaluable. 如果您有更好的主意-一个例子将是无价的。

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() . 由于您的数组似乎是常量数据,只能用来控制CreateToolTip()功能,因此最明智的方法是将两个(或多个)数组定义为静态常量,并在其中使用指向一个或另一个的指针。 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. 这样,您可以避免不必要的数据复制,不必要的数组初始化,不必要的内存分配,以及避免尝试处理可变数组大小的不必要麻烦。

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. 如果使用的是C ++ 11,则可以在类定义本身中初始化成员。 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++. 它是在C中使用malloccalloc以及在C ++中使用new来完成的。

Or, you can use some data type which manages dynamic memory allocation itself whose example is std::vector in C++. 或者,您可以使用某种数据类型来管理动态内存分配本身,其示例在C ++中为std::vector

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM