views:

249

answers:

2

Is it possible to use const parameter to CArray

I am currently using CArray like this but it won't compile:

typedef CArray<const CString, const CString&> data_container;

And I always get this compile error :

error C2664: 'ATL::Checked::memcpy_s' : cannot convert parameter 1 from 'const CString *' to 'void *'

A: 

Apparently no. Why would you want to do that?

avakar
Those CString are not supposed to change, so const seams appropriate.
Drahakar
Does that mean you want to allow inserts and deletes, but disallow modifications?
avakar
+2  A: 

The code that CArray uses expects your TYPE to be non-const, so it can cast to void* (as noted by the compilation error message).

You could store const CString pointers, which would give you a const CString when dereferenced. You do have the burden of allocating/cleaning up that memory now. An alternative is to wrap a CString in a simple class, that has a "GetString" function that returns a const reference to its internal CString instance.

KSchmidt
In other words, CArrays can modify their contents in various ways (loading from serialization, copying from another, etc), so the contents cannot be const.
KSchmidt
Is there an alternative that would allow const content?
Drahakar
See my edited answer for some alternatives.
KSchmidt