Solved

CStringArray copy constructor????

Posted on 2004-03-27
6
1,425 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
  • 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

856 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