Can I initialize array in constructor of array?

D.D.K-2637 966 Reputation points
2021-07-17T19:53:23.18+00:00

Normally, I write the these code follow instructions:

COleSafeArray array;
DWORD numElements[] = { row};   //
array.Create(mytype, 1, numElements);

then to make easily, I create wrap class inherit COleSafeArray, my code like this:

myclass::myclass(int Rows)
    : COleSafeArray()
{
   DWORD numElements[] = { row };   //
   Create(mytype, 1, numElements);
}

Let's say I have an Object, from this Object I have 1D array,
so my code now like this:

myclass::myclass(Object* obj)  
    : COleSafeArray()
{
   DWORD numElements[] = { row };   //
   Create(mytype, 1, numElements);
   //From obj -> get 1D array then use PutElement method to fill this base
   //..
   PutElement(index, str); <<-- error
}

but, I get this error:
Microsoft C++ exception: COleException at memory location 0x0000006A353FE5D8
Can I create an array by only constructor?
How to make this code works: myclass arr(obj);

Thanks you!

Developer technologies C++
{count} votes

Accepted answer
  1. RLWA32 49,536 Reputation points
    2021-07-18T15:53:37.557+00:00

    This test case worked without any problems for me whether or not the class derived from COleSafeArray loaded the SAFEARRAY in the constructor or in a subsequent function call after construction.

    class Object {
    public:
        Object() = default;
        ~Object() = default;
    
        CString* getArray()
        {
            return m_arr;
        }
    
        int getElements()
        {
            return ARRAYSIZE(m_arr);
        }
    
        CString m_arr[5]{ L"one", L"two", L"three", L"four", L"five" };
    };
    
    class CWrapOle : public COleSafeArray
    {
    public:
        CWrapOle(Object* obj, bool bLoad) : m_bLoad(bLoad)
        {
            DWORD dwElements = obj->getElements();
            Create(VT_BSTR, 1, &dwElements);
            if (bLoad)
            {
                long ndx[1]{};
                CString* pArray = obj->getArray();
                for (DWORD i = 0; i < dwElements; i++)
                {
                    ndx[0] = i;
                    PutElement(ndx, CComBSTR(pArray[i]));
                }
            }
        }
    
        void LoadArray(Object* obj)
        {
            if (!m_bLoad)
            {
                long lBound{MAXLONG}, uBound{MAXLONG};
                DWORD dwElements = obj->getElements();
                GetLBound(1, &lBound);
                GetUBound(1, &uBound);
                ASSERT(dwElements == uBound - lBound + 1);
                long ndx[1]{};
                CString* pArray = obj->getArray();
                for (DWORD i = 0; i < dwElements; i++)
                {
                    ndx[0] = i;
                    PutElement(ndx, CComBSTR(pArray[i]));
                }
            }
        }
    
    private:
        bool m_bLoad{ false };
    };
    

    If you want to load the array in the derived class constructor pass "true", otherwase pass "false".


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.