Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > counfused.. about rule of 3...

Reply
Thread Tools

counfused.. about rule of 3...

 
 
SpreadTooThin
Guest
Posts: n/a
 
      06-19-2008
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 it simple to make sure that the string that
is used as a constructor has an even length.
The class is called a uid.
I'm a little confused.. should this just inherit from std::string ie
class uidublic std::string ?
I'm not sure I'm handling the copy constructors properly.

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)
{
id = _id;
if (id.size() & 1) // is the length odd?
id.append(" ");
}
uid& operator=(uid& _id)
{
id = _id;
}
~uid(void)
{
// Nothing to do really.
}
};

 
Reply With Quote
 
 
 
 
acehreli@gmail.com
Guest
Posts: n/a
 
      06-19-2008
On Jun 19, 12:51 pm, SpreadTooThin <bjobrie...@gmail.com> wrote:
> I am developing a class. It's a pretty simple one.
> The class has one data member which is a std::string.


If a class consists only of members that know how to take care of
their resources (e.g. std::string), you usually don't need to do
anything yourself.

> The purpose of the class it simple to make sure that the string that
> is used as a constructor has an even length.
> The class is called a uid.
> I'm a little confused.. should this just inherit from std::string ie
> class uidublic std::string ?


No.

> I'm not sure I'm handling the copy constructors properly.


You don't need to provide a copy constructor or a destructor in this
case.

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


Do you really have to assign a std::string? I don't think so, because
you can always assign from a uid, which is guaranteed to have 'id' of
even length.

> {
> id = _id;
> if (id.size() & 1) // is the length odd?
> id.append(" ");
> }
> uid& operator=(uid& _id)


You don't need to provide that assignment operator, because the
compiler generated one does the same thing:

> {
> id = _id;
> }


Actually, the compiler generated one would do better by assigning even
the bases, if you had any.

On the other hand, the compiler generated one would not be exception
safe, if you had more than one object in this class. Because the C++
standard doesn't define the generated operator= be exception safe. It
is possible that the object is left in a half-assigned state if one of
the member assignments throws. But you don't have a second member
here.

> ~uid(void)
> {
> // Nothing to do really.
> }
>
> };


Start with the following simple class. I bet that works the way you
need:

#include <string>

class uid
{
std::string id;

public:

explicit uid(const std::string & _id)
:
id(_id)
{
if (id.size() & 1) // is the length odd?
id.append(" "); // yes. Now the length should be even
}
};

int main()
{
uid u("abc");
u = uid("xyz");

// Remove 'explicit' above to be able to compile the following as well
// std::string s("123");
// u = s;
}

Ali
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      06-20-2008
On Jun 19, 9:51 pm, SpreadTooThin <bjobrie...@gmail.com> wrote:
> 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 it simple to make sure that the string
> that is used as a constructor has an even length.
> The class is called a uid.
> I'm a little confused.. should this just inherit from
> std::string ie class uidublic std::string ?


Probably not. You don't want to support all of the interface of
std::string: appending a single character to your uid would
break the invariant, for example.

> I'm not sure I'm handling the copy constructors properly.


There's nothing you have to do. If the only data member of your
class is an std::string, the compiler will handle the copy
constructor, assignment and the destructor correctly. You don't
need any of the three.

> 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)
> {
> id = _id;
> if (id.size() & 1) // is the length odd?
> id.append(" ");
> }


Note that this is NOT a copy assignment operator, so the
compiler will still provide the copy assignment operator you
need. (And of course, you forgot the return at the end.)

> uid& operator=(uid& _id)
> {
> id = _id;
> }


Which is exactly what the compiler provided copy assignment will
do. Almost: in the compiler provided version, the parameter
will be a cosnt reference (which it should be), and the compiler
won't forget the return.

> ~uid(void)
> {
> // Nothing to do really.


So why bother providing it?

> }
> };


--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to add validation rule for url in the validation-rule.xml ,I added some thing like this but......... shailajabtech@gmail.com Java 0 10-12-2006 08:36 AM
Can Thunderbird or Mozilla enforce same message rule Big Craigie Firefox 3 03-01-2005 12:43 AM
translation-rule question Steve Ames Cisco 0 11-21-2003 10:50 PM
Creating a simple rule using PDM 3.0(1) Corbin O'Reilly Cisco 2 11-20-2003 03:15 AM
default outbound rule in a PIX 501 Eric Sabine Cisco 2 10-17-2003 04:56 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57