Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Const pointer to an array of pointers

i have this class

struct A {
    A();
    Item*  m_Items[ON_CPU + ON_GPU];
    Item** m_ItemsOnCpu;
    Item** m_ItemsOnGpu;
};

I need to initialize

m_ItemsOnCpu to m_Items

and

m_ItemsOnGpu to m_Items + ON_CPU

So I need const pointers to two parts of the array. How do I need to declare and then initialize them?

like image 646
Yola Avatar asked Aug 26 '26 21:08

Yola


1 Answers

In C++11 you can just do:

struct A {
    A();
    Item*  m_Items[ON_CPU + ON_GPU];
    Item** const m_ItemsOnCpu = m_Items;
    Item** const m_ItemsOnGpu = m_Items + ON_CPU;
};

On other versions of C++, use an initialization list:

struct A {
    A() : m_ItemsOnCpu(m_Items), m_ItemsOnGpu(m_Items + ON_CPU) {};
    Item*  m_Items[ON_CPU + ON_GPU];
    Item** const m_ItemsOnCpu;
    Item** const m_ItemsOnGpu;
};
like image 190
Carl Norum Avatar answered Aug 29 '26 11:08

Carl Norum