CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  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.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Rule of 3 for a simple class...

    The rule of three does not apply here since the compiler generated copy constructor, copy assignment operator and destructor will suffice. All you need is a constructor that takes a string argument and applies the check for an even length.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: Rule of 3 for a simple class...

    what exactly is the rule of three? I can't google a satisfying explanation ;-)

  4. #4
    Join Date
    Nov 2006
    Posts
    103

    Re: Rule of 3 for a simple class...

    Yes I guess your right..
    As none of the properties are pointers to data....
    Right?

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Rule of 3 for a simple class...

    what exactly is the rule of three?
    If you define the copy constructor, copy assignment operator, or destructor, you probably have to define all three to be correct. Of course, there are exceptions, e.g., if you merely define the destructor so as to declare it virtual for a polymorphic base class.

    Yes I guess your right..
    As none of the properties are pointers to data....
    Right?
    Yes, and neither do you have reference or const members.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Feb 2003
    Posts
    377

    Re: Rule of 3 for a simple class...

    Quote Originally Posted by Richard.J
    what exactly is the rule of three? I can't google a satisfying explanation ;-)
    If any one of the compiler-generated destructor, copy constructor or copy assignment operator are inappropriate for a class, then it is likely that all three will compiler-generated functions will be inappropriate. The writer of the class would then be required to write their own or disable them.

    Note that in modern C++ with the use of other classes that manage resources themselves it is less likely that the rule of three will apply to a well-written class.

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