CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Threaded View

  1. #1
    Join Date
    Nov 2006
    Posts
    103

    Rule of 3 for a simple class...

    I am developing a class. It's a pretty simple one.
    The class has one data member which is a std::string.
    The purpose of the class is simply to make sure that the string ends up having an even length.
    The class is called a uid.
    I'm a little confused.. should this just inherit from std::string ie
    class uid:public std::string ?
    I'm not sure I'm handling the copy constructors properly.
    Code:
    class uid 
    { 
    private: 
       std::string id; 
    public: 
       uid(std::string _id) 
       { 
          id = _id; 
          if (id.size() & 1) // is the length odd? 
             id.append(" ");  // yes. Now the length should be even 
       } 
       uid& operator=(std::string& _id) // Is this method necessary?
       { 
          id = _id; 
          if (id.size() & 1) // is the length odd? 
             id.append(" "); 
       } 
       uid& operator=(uid& _id) 
       { 
          id = _id; 
       } 
       ~uid(void) 
       { 
          // Nothing to do really. 
       }
       int getSize(void)
       {
          return id.size();
       }
       const void *getData(void)
       {
          return id.c_str();
       }
    };
    Last edited by JustSomeGuy; June 19th, 2008 at 03:06 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured