Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In MFC CArray, what are reasons to use different default template type?

Tags:

c++

templates

mfc

The MFC CArray class has two template parameters (from MSDN):

template < class TYPE, class ARG_TYPE = const TYPE& > 
class CArray
...

The default parameter for ARG_TYPE is const TYPE&, and some questions on SO ask when to use a type different from the default (e.g. Should the ARG_TYPE for a CArray be const & or not, or What's the difference between CArray and CArray ?).

My questions are now: Are there reasonable use cases of CArray where the ARG_TYPE isn't TYPE or const TYPE&, e.g. another class? What are the reasons for these usages?

like image 229
vividos Avatar asked Jul 14 '11 08:07

vividos


2 Answers

My guess is that, it should be related to inheritance hierarchy.

struct B {};
struct D : B {};

CArray<D, const B&> myArray;

You may want to have the base class as the receiving argument, which can receive a const reference from the derived class.

like image 96
iammilind Avatar answered Oct 30 '22 12:10

iammilind


My best example is CArray <CString, LPCTSTR>. In fact, I recall CArray <CString, CString&> didn't work very well...

like image 22
djeidot Avatar answered Oct 30 '22 12:10

djeidot