Hi,
I want to make an array of strings like this:

string s[4]={"a", "ab", "abc", "abcd"};

Then I want to write a class with a pointer to a string (and not an array of string pointers).

class someclass{ 
public:
// constructors and other member functions here
private:
string *sPtr;
int numElements;
};

I want to pass the string, s, to the constructor for the class I wrote (and maybe even use it in the initializer list) and then use the class object's pointer to s to find the length of each element of array s.

someclass::someclass(int arraysize, string s[]): numElements(arraysize), sPtr(s)
{
//do stuff
numElements=arraysize;
sPtr=&s;
}

In the function where I declared s I would do this:

someclass myobject(4, s); // string s[4]={"a", "ab", "abc", "abcd"}; from above

In a member function of class someclass I would do something like this:

cout<<sPtr[1].length()<<endl; //expect to get the size of s[1]

Obviously this didn't work or I wouldn't have posted ;)
I searched online for quite a while but most people prefer to use char* and those that do deal with <string> don't even bother with arrays of strings, their tutorials are too basic.
There are so many places where I could be going wrong :S
Thanks for reading my thread.

Recommended Answers

All 8 Replies

>>sPtr=&s;
Wrong. should be sPtr = s; . But you really don't need that line, or the previous line, at all since both variables are being initialized on line 1 of the code snippet you posted.

This worked for me

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

class someclass{ 
public:
someclass::someclass(int arraysize, string s[]): numElements(arraysize), sPtr(s)
{
}

friend ostream& operator<<(ostream& out, someclass& c);
private:
string *sPtr;
int numElements;
};

ostream& operator<<(ostream& out, someclass& c)
{
    for(int i = 0; i < c.numElements; i++)
        cout << c.sPtr[i] << '\n';
    return out;
}

int main()
{
string s[4]={"a", "ab", "abc", "abcd"};
someclass c(4,s);
cout << c << '\n';
}

Thank you for your help. It runs but gives me a run time error similar to one I had before. For some reason the strings contain garbage and therefore have sizes that are 6 digits long. I assume this has something to do with passing the variable to the pointer in the constructor, like the scope of the variable is lost after I point to it and the memory is getting reused for something else maybe.
Also, I should point out that it is a member function of someclass that is reading the length/printing out the string[] elements.
Thanks again.

OMG, just use vectors :

#include <string>
#include <vector>
#include <iostream>

using namespace std;

class StringArray{
private:
 std::vector<string>  strArray;
public:
 StringArray(const std::vector<string>& vecStr): strArray(vecStr){};
 void print(){ /* print vector here */;
};

int main(){
 std::vector<string> vec(5,"hello"); //contains 5 of "hello"
 StringArray strArray(vec);
 vec.print();
}

>>It runs but gives me a run time error similar to one I had before
It will depend on where you declared those strings. If you declared them in main() like the code I posted then there should not be a problem. On the otherhand if you declared them in some other function and tried to use them after that function returned then all those strings will have been destroyed and invalidated all those pointers.

As firstPerson mentioned the best practice would be to use a vector of strings in that class and not a pointer so that the class owns the memory of all those strings. That way you won't have to worry about the strings going out of scope.

Thanks for the help gentlemen. I'm determined to do this with the pointer though, vector brings too many member functions with it, not that I would use it enough to make a big difference. I'll use it if I have to but first I'll work with dynamically allocating the strings and then passing them.
Thanks again.

I did this:

string* strList = new string[4];//={"a", "ab", "abc", "abcd"};
	  strList[0]="a";
	  strList[1]="ab";
	  strList[2]="abc";
	  strList[3]="abcd";
	  currentMenu = new MenuObject( 4, strList);//listPtr);

Then I just changed the constructor to take a pointer instead of the array. It works fine.
Thanks again for the help guys, this thread is done ;)

I bet you have a memory leak. If you are going to use pointers, then use "smart pointers".

No memory leak, I'm using delete ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.