• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3111
  • Last Modified:

Good example of CArray

I would like to have a good example (complete code) of the use of CArray, used to create a dynamic array:

How to declare it.
How to initialize it.
How to put new elements in it.
How to access its elements.

In my case, it would be an array of structs. Inside each struct I have only two CString fields.
0
javigil1
Asked:
javigil1
1 Solution
 
paulburnsCommented:

// .h
// This is your array element class
class CTwoStrings
{
public:
 CTwoStrings() {m_strOne=_T(""); m_strTwo=_T("");};
 ~CTwoStrings() {};

 CTwoStrings(const CTwoStrings& ref);
 CTwoStrings& operator=(const CTwoStrings& from);

 CString m_strOne;
 CString m_strTwo;
}

// .cpp
// and its implementation
CTwoStrings::CTwoStrings(const CTwoStrings& ref)
{
 *this = ref;
}

CTwoStrings& CTwoStrings::operator=(const CTwoStrings& from)
{
 m_strOne = from.m_strOne;
 m_strTwo = from.m_strTwo;
}

// Declare the array like this...
CArray<CTwoStrings, CTwoStrings&> array;

// and use it as ghim... has shown.
0
 
RONSLOWCommented:
use CArray<CString,CString> or CArray(CString,const CString&> instead of CArray<CString,int>.

Read the docs on CArray to see what the two CArray template parameters mean.  You seem to think that making the second one 'int' means you'll be subscripting with an int.  This is not the case, CArrays ALWAYS subscript with an int.  The second arg is the type to use as args for any of the CArray functions.  It will usually be either the same type as the data (1st param) or a const& for the same type.  For simple data, same type is ok, for structs use the const&
0
 
caiman23Commented:
Hey, Paulburns:

Shouldnt this return something?

CTwoStrings& CTwoStrings::operator=(const CTwoStrings& from)
{
 m_strOne = from.m_strOne;
 m_strTwo = from.m_strTwo;
}
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now