0

Can I initiate a string array and pass it as a function that initializes it. I know this sounds redundant, but basically I want to initiate a string array and then pass it to a variable so that I can use it later? Something like this:

This is the .h:

class ScreenBasics{

 void setupAnswers(int &_numberOfAnswers, string *_answersText);

 string *answersText;


{

This will be the implementation .cpp

void ScreenBasics::setupAnswers(int &_numberOfAnswers, string *_answersText){

 answersText = _answersText; // this is where I get confused cause I don't know if I should initiate my string pointer using new like answersText = new string(_numberOfAnswers);

{

so in the main.cpp I can do something like this:

 int main( ) {

 ScreenBasics basics;
 int numberOfAnswers = 4;
 string newAnswers [] = { "Good", "Superb", "Great", "Perfect"};
 basics.setupAnswers(numberOfAnswers, newAnswers);

 // let's say I want to call some of those answers later
 for ( int i = 0; i < numberOfAnswers; i++){

  cout << basics.answersText[i] << endl;

 }

}

Thanks!

mauricioSanchez
  • 366
  • 10
  • 23

2 Answers2

1

Have you thougt about using structs? Both classes and structs can have a mixture of public and private members, can use inheritance, and can have member functions. I would recommend using structs as plain-old-data structures without any class-like features, and using classes as aggregate data structures with private data and member functions.

Your code would look like the following:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct movies_t {
  string title;
  int year;
} mine, yours;



int main ()
{
  string mystr;

  mine.title = "2001 A Space Odyssey";
  mine.year = 1968;

  cout << "Enter title: ";
  getline (cin,yours.title);
  cout << "Enter year: ";
  getline (cin,mystr);
  stringstream(mystr) >> yours.year;

  cout << "My favorite movie is:\n ";
  printmovie (mine);
  cout << "And yours is:\n ";
  printmovie (yours);
  return 0;
}

 void printmovie (movies_t movie)
 {
    cout << movie.title;
    cout << " (" << movie.year << ")\n";
 }

Please let me know if you have any questions!

Devarsh Desai
  • 5,984
  • 3
  • 19
  • 21
  • @Devardh Desai I finally understood your code. The reason why I want to have control of this the way I needed it, is because that class ScreenBasics, in some screens, has more than four answers. That's why the way the information gets stored is what matters. So I want to be able to have different classes with different numbers of strings. I actually ran my code and it worked. I do not come from a CS background,and some times even simple concepts are hard to grasp. Perhaps I should have been more daring and tried my approach. Hope that makes sense. – mauricioSanchez Sep 10 '14 at 04:50
  • 1
    hey mauricioSanchez, ahh i see! would you like me to re-write the above example using classes and setters and getters? :-) – Devarsh Desai Sep 10 '14 at 04:53
  • Thanks! I think the other answer supplies exactly what I needed – mauricioSanchez Sep 10 '14 at 12:22
0

It sounds like your class should use a constructor to initialize the pointer.

class ScreenBasics
{
    ScreenBasics(std::string* _answersText, std::size_t size)
        : answersText(new std::string[size])
    {
        // copy values from _answersText to answersText
    }
};

Note that since you are allocating a resource dynamically, you will need to implement The Rule of Three: that is, you need to create a copy-constructor, copy-assignment operator and destructor. This is because their default implementations do not semantocally conform to our requirements. For instance, the default copy-constructor and copy-assignment operator perform shallow copies (that is, they copy the pointer but not what it points to). Also, the destructor doesn't free the memory allocated my new[]. You will need to provide your own definition of the destructor that calls delete[].

Fortunately, all this can be avoided by using the standard library container std::vector, which is a dynamic array class that handles the memory allocation for you. The default implementations of the aforementioned constructors will correctly copy/copy-assign a vector if the need be:

class ScreenBasics
{
    ScreenBasics(std:vector<std::string> _answersText)
        : answersText(_answersText)
    {
    }
};

Notice that the size also didn't have to be passed as a parameter. A vector maintains its size internally.

Community
  • 1
  • 1
David G
  • 94,763
  • 41
  • 167
  • 253