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??

siddhant3s commented: First post, Coding Tags. Good style of questioning +3

Recommended Answers

All 4 Replies

Hmmm, I have seen the exact question before on this forum.
Anyways, first of all I am glad that you have used code tags on your first post.

>>BUT i don't know how I should set the Mem pointer
In the constructor, you should use new operator to assign required memory(determined by the variable called Capacity) to Mem Mem=new char[capacity]; Then you should perform copy of the char[] argument to the Mem by either strcpy() {needs cstring header file} or by character by character copy.

Copy constructor and operator= can be done in the same way.

Remember to delete[] Mem every time you assign it with a new chunk of memory

ZOMG Thanks! I haven't tried it yet, but I had a feeling it would require the new operator. I'll be able to work on it when I get to work, but thanks for the help!

>>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??
I guess you would be equating the two pointers Mem=other.Mem; //dont do this Don't do this. Use strcpy or by character by character copy.

I know this is a bit off topic now, but I'm having troubles with my '<<' operator. I'm getting segment faults. For some reason, I can output letters of the string using a predefined class function ( cout << aString[0] << endl; returns first letter of aString ), but I get seg. faults using while loops to iterate through the aString ( while(aString != '\0' ). Any suggestions?

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.