For getting the size, you can already use the strlen function:
size_t sz;
sz = strlen(s);
You can usenew[] and delete[] for the memory allocation to hold the desired string.
After that, you copy the string passed via the argument to the newly allocated memory, use strcpy.
Note: Within your object you'll do some dynamic memory allocation.
You'll probably want to implement a destructor, a copy constructor, and overload the assignment operator as well.
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
You probably already know this but don't forget the +1 in the allocation for storing the terminator.
sz = strlen( s );
char *p = new char[ sz+1 ]
wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
Corrections:
MyString::MyString(const char* s)
{
<strong>// don't forget the type:</strong>
<strong>size_t</strong> stringSize = strlen(s);
<strong>// you need a pointer to the newly allocated memory:</strong>
<strong>// don't forget to allocate memory for the null-terminator as well:</strong>
<strong>char *p</strong> = new char[stringSize<strong>+1</strong>];
strcpy (<strong>p</strong>, s);
}
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
What Tux4Life said!
But would make sense to store the string size so future strlen() requests of your string in class only needs to return the value. Not look it up!
MyString::MyString(const char* s)
{
stringSize = strlen(s);
szString = new char[ stringSize + 1];
strcpy ( szString, s );
}
wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
>For my stringSize I have the type declared as a char* in my header file so I would use that instead of size_t correct?
Nope, to store a size, you don't use a char pointer, instead you use size_t (you can compare it to an unsigned int).
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
Without listing the first few errors you receive it's not possible to say with surety what you have going. However, the object you have used called this is a pointer and rightOp is apparently a C styled string. As such you can't derefernce a C styled string. What you probable want is to see if the address stored in this and the address of rightOp are the same address (ie, are the same object).
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
Yup, that's what the first error message says to me. You could try something like this:
if(this != rightOp)
sinc the name of an array acts as an address for the first element of that array. Hopefully they will allow comparisons of two addresses. If that doesn't work, then someone else can probably give you the correct version.
Alternatively, since this will never be a simple C Style string it can never have the same address as a C Style string and therefore you could probably get by without the comparison. If you are assigning one MyString to another, however, then it could in theory be the same address, so looking for identity is recommended.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
>It should set the size of the string to the number of characters in the C-style string (not including the null character), allocate a string text array of that size, and then copy the characters of the C-style string into the string text array.
Why would someone want to do that?
You have to allocate space for the null terminator as well.
Otherwise you'll run into problems while displaying the string using cout for example.
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
I am certainly no expert in STL, but it is my understanding that the const char * embedded in the STL string class isn't necessarily implemented as a C style string----that is, it isn't necessarily a null terminated char array. To output such an object you could overload the << operator using a loop in the method body to display each char separately, just like you could use a loop rather than strcpy() to assign/copy the object. I suspect if you look under the hood of strcpy() you will find a loop as well, but we're even further out of my surety level on that speculaton. The only reason to do this that I can think of is to make the STL string object independent of any C coding requirements. Independent confirmation not a bad idea before spreading this around too far is a good idea.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
Well, as long as you hold the correct number of characters in a variable, this would be no problem, but a null-terminator takes 1 byte extra space, and you won't have to add several other routines to make the string display correctly on the screen.
But, of course, if his assignment is to do it without a null-terminator, then my advise probably doesn't make much sense :)
>The only reason to do this that I can think of is to make the STL string object independent of any C coding requirements.
Yeah, probably his teacher wants that he avoids functions from the standard function library.
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243