?
Solved

Good example of CArray

Posted on 2000-03-26
3
Medium Priority
?
3,087 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 300 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

649 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