?
Solved

CStringArray copy constructor????

Posted on 2004-03-27
6
Medium Priority
?
1,538 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 200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.
Suggested Courses

801 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