I've been wracking my brain over the last week. I have project due soon, but I'm having a lot of trouble with certain aspects of pointers in class instances. My project is to re-design the String class library by remaking basic functions. I was given a header file and told not to alter it in any way what so ever.
My class is declared as following:
class String
{
private:
unsigned Capacity; //Number of memory locations reserved.
unsigned Length; //Number of memory locations used.
char * Mem; //Pointer to memory to hold characters.
public:
String() //Constructor for empty string.
{
Mem = Null;
Capacity = 0;
Length = 0;
}
String( const String& ); //String from existing string.
String( const char[] ); //String from char sequence.
String& operator=( const String& ); //Copy string
}
// Other boring operators including stream operators.
My main method for testing the functions is by making a character sequence and then setting an empty object of type String equal to the String(charSequence).
The character sequence to String is defined by getting the length of the sequence, getting the capacity, and then setting the Mem pointer. BUT i don't know how I should set the Mem pointer.
The character sequence is passed as a constant so, i first tried making a copy of the sequence in the Class member function and declaring Mem = charStringCopy. What I need to do is pass a Mem pointer from one String object to another String object so that both String objects refer to the same character sequence.
However, each time I attempt to pass the Mem pointer from one String obj to another, I fail at doing it right or producing the right results. I haven't had a problem passing Length and Capacity, just the Mem pointer. How should I define the Mem pointer so that I can pass it between String objects so I can refer to the same character sequence??