Solved

CStringArray copy constructor????

Posted on 2004-03-27
6
1,463 Views
Last Modified: 2007-12-19
I wrote a function that returns a CStringArray but got error sayin that no copy constructor.

So i learnt i need to derive a class from CStringArray and make a new copy constructor

so what is a CStringArray copy constructor and How to rewrite a copy constuctor? can anyone teach me with an example?
0
Comment
Question by:punktux
[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
  • 3
6 Comments
 
LVL 8

Accepted Solution

by:
mnashadka earned 50 total points
ID: 10696103
An easier way to handle this would be to have the user pass the CStringArray in as a reference parameter, then you fill it up, rather than returning it from the function:

// User's code
CStringArray sa;
MyFunc(sa);

// Your Function
void MyFunc(CStringArray &sa)
{
  sa.Add("First String");
  sa.Add("Second String");
}
0
 

Author Comment

by:punktux
ID: 10696113
the function i am writing need to return an array(of strings) for furture use. So can u teach me how to do the copy constructor?
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 10696135
Passing the CStringArray in as a reference parameter (with the &) is just like passing it in as a pointer (with some semantic differences), so after it leaves your function, it will still have the data in it.  If you really feel the necessity to derive a class from CStringArray and implement a copy constructor, it would look something like:

class MyStringArray : public CStringArray
{
  MyStringArray() {} // Default constructor
  MyStringArray(const MyStringArray &msa)
  {
    Append(msa);
  }

  // Now expose all of the other member functions
};
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:punktux
ID: 10696157
for your first advice, sorry for my newbieness

// Your Function
void MyFunc(CStringArray &sa)
{
  sa.Add("First String");
  sa.Add("Second String");
}

how to obtain the string in the array in anywhere else?i mean the "First String"
will cout<< sa ; give "First String" and Seoncd String?
0
 

Author Comment

by:punktux
ID: 10696183
i use the referenced array in OnInitDlg in MFC to create a series of static text

for example

CStatic *m_pStaticArray = new CStatic;
                  
m_pStaticArray[j].Create(_T("ID"), WS_CHILD|WS_VISIBLE|SS_CENTER,
      Rect(2,b+7,30,b+20), this, IDC_STATIC1+j);
      }

how to access the array "sa" and make a loop to create the static texts.?
0
 
LVL 8

Expert Comment

by:mnashadka
ID: 10698789
I'm not sure exactly how you're planning on displaying it, but if you wanted it on separate lines (you would have to modify the CStatic to be big enough), it would look something like:

CStringArray sa;
MyFunc(sa);

CStatic *m_pStaticArray = new CStatic;
               
m_pStaticArray[j].Create(_T("ID"), WS_CHILD|WS_VISIBLE|SS_CENTER,
     Rect(2,b+7,30,b+20), this, IDC_STATIC1+j);

// Create the string to display
CString display_text;
for(int i = 0; i < sa.GetCount(); ++i)
{
  // If it's not the first element, append an end of line
  if(i > 0)
    display_text += "\r\n";
  // Append the next element
  display_text += sa[i];
}

m_pStaticArray->SetWindowText(display_text);

Good luck.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

739 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