CArray query [const problem] 
Author Message
 CArray query [const problem]

I have a class with a member of type CArray<MyClass,MyClass&> I have a
member function that returns a constant reference to this CArray

const CArray<MyClass,MyClass&>& AClass::GetArray()

The problem is that when I try to use this const reference to get at the
data I find that the constant function to access the array doesn't
return a constant reference as expected it returns a copy of the element

TYPE operator []( int nIndex ) const;

Obviously I could const_cast away the constness of the return array but
this is a bit naff.  Any suggestions?



Sat, 25 May 2002 03:00:00 GMT  
 CArray query [const problem]
Hi !

        This is a well known feature of MFC collection classes. And I agree
that it doesn't make any sense. The only suggestion I have is the
following: create your own template class (CMyArray) on top of MFC's
CArray and rewrite ElementAt and operator[] methods so that they return
'const' reference to element instead of copy of element from their
'const' versions:

        const TYPE& ElementAt(iIndex) const
          { return m_pData[iIndex]; }
        const TYPE& operator [](iIndex) const
          { return ElementAt(iIndex); }

Quote:

> I have a class with a member of type CArray<MyClass,MyClass&> I have a
> member function that returns a constant reference to this CArray

> const CArray<MyClass,MyClass&>& AClass::GetArray()

> The problem is that when I try to use this const reference to get at the
> data I find that the constant function to access the array doesn't
> return a constant reference as expected it returns a copy of the element

> TYPE operator []( int nIndex ) const;

> Obviously I could const_cast away the constness of the return array but
> this is a bit naff.  Any suggestions?



Sun, 26 May 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. CArray of classes with an CArray inside problem

2. CArray::operator[] (int) const

3. problem with array of const pointers to const data

4. const pointer to const data problem

5. Help: inline char const* const& max(char const* const &a, char const* const &b)

6. CArray within CArray?

7. Sorting a CArray or another class that combines CMap and CArray functionality

8. MFC/C++ Guru needed: A CArray of CArrays

9. CArray of CArrays???

10. how to make CArray of CArray ?

11. A CArray of CArrays

 

 
Powered by phpBB® Forum Software