Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return a CStringArray gives errors

Im trying to return a CStringArray: In my ".h" I defined:

    Private:
    CStringArray array;

    public:
    CStringArray& GetArray();

In . cpp I have:

    CQueue::CQueue()
    {
    m_hApp = 0;
    m_default = NULL;
    }


    CQueue::~CQueue()
    {

     DeleteQueue();
    }

    CStringArray& CQueue::GetArray()
    {

     return array;   
    }

From another file I'm trying to call it by:

    CStringArray LastUsedDes = cqueue.GetArray();

I guess it is because of the above line that I get the error:

   error C2248: 'CObject::CObject' : cannot access private member declared in class 'CObject'
like image 935
user1563551 Avatar asked Aug 06 '12 18:08

user1563551


1 Answers

The problem is on this line

CStringArray LastUsedDes = cqueue.GetArray();

Even though you're returning a reference to the CStringArray in the GetArray() function a copy of the array is being made in the line above. CStringArray itself doesn't define a copy constructor and it derives from CObject, which has a private copy constructor.

Change the line to

CStringArray& LastUsedDes = cqueue.GetArray();

But be aware that LastUsedDes now refers to the same CStringArray contained in your class instance, and any changes made to one will be visible in the other.

If you need a local copy of the returned array you can use the Append member function to copy the contents.

CStringArray LastUsedDes;                // default construct the array
LastUsedDes.Append( cqueue.GetArray() ); // this will copy the contents of the
                                         // returned array to the local array
like image 190
Praetorian Avatar answered Sep 30 '22 17:09

Praetorian