Solved

Good example of CArray

Posted on 2000-03-26
3
3,003 Views
Last Modified: 2013-11-20
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
Comment
Question by:javigil1
3 Comments
 
LVL 2

Expert Comment

by:paulburns
ID: 2659234

// .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
 
LVL 10

Accepted Solution

by:
RONSLOW earned 100 total points
ID: 2659406
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
 

Expert Comment

by:caiman23
ID: 2659864
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Login Script to Copy Folders 12 42
post4 challenge 28 104
Fibonacci challenge 11 132
Thin secure Windows 10 5 102
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question