Solved

Good example of CArray

Posted on 2000-03-26
3
2,965 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countEvens challenge 2 58
isEverywhere  challenge 19 64
ShiftLeft challenge 21 66
Thin secure Windows 10 5 48
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now