Thread: Custom String Class member functions

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    37

    Custom String Class member functions

    Ok I posted something earlier about this but was asking for too much at once so I am going to try to break this up now as I find myself needing help.

    Being a beginner to C++, I am right now stuck on declaring the member functions in the header file.

    Right now I need to create an assign member function that accepts a string object parameter. This checks for the assignment to itself and then assigns a new value to this String from the parameter String, making a deep copy of the character data.

    So in my header file this would be declared as something like:
    Code:
    #ifndef _TSTRING_H
    #define _TSTRING_H
    
    class TString { // Prefix with 'T' for uniqueness
    public:
      TString(const char *pText = 0); // default ctor
      TString(const TString &);
      ~TString();
    
      int length()
      {
    	  return mLength;
      }
    
      const char *asChar() const;
    
      void assign(string obString); //<---- member function in question
    
    private:
      int mLength; // length of char data (not including null byte)
      char *mpText; // pointer to dynamic char data in heap
    };
    
    #endif   // _TSTRING_H
    im going to worry about defining the member fuctions after i finish declaring them in the header, so im just looking to get this part done right now.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    o and to add to this. the next step calls for another assign member function this time that accepts a character pointer. this one checks for assignment to self, and assigns a new value to this string from the parameter character array.

    is it possible to have two member functions with the same name in the header file?

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    37
    ok so now i think in accordance to the second post the two assign member functions would be declared like this:

    Code:
      void assign(string obString);
      void assign(const char *);

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You've deleted the specification in your other thread. Is assign supposed to take a std::string (rather than another TString)? If so, I can't see what self-assignment checks you are talking about. How can your TString instance be the same as a std::string instance?

    As for the implementation, you should try to implement functions in terms of other functions. The assign function, as far as I can understand, does exactly the same as operator= (which you will need to write anyway), so you can just implement it like that:

    Code:
    void TString::assign(const TString& source)
    {
        *this = source;
    }
    As far as self-assignments go: you don't need to worry about them if you allocate new memory first and copy the string, and only then deallocate the previous buffer (this also increases exception safety). There is also a well-known copy-and-swap idiom which you might use to implement operator= (use Google).

    As for the other things, you seem to be aware of making methods const. The length method should be const too.
    Last edited by anon; 10-03-2009 at 09:42 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 09:54 AM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM