Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

CStringArray copy constructor????

Posted on 2004-03-27
6
Medium Priority
?
1,582 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
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.

 

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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

598 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