Solved

CStringArray copy constructor????

Posted on 2004-03-27
6
1,360 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:punktux
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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.

762 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

9 Experts available now in Live!

Get 1:1 Help Now