473,406 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

counfused.. about rule of 3...

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 uid:public 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.
}
};

Jun 27 '08 #1
3 1127
SpreadTooThin 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 uid:public std::string ?
No.
I'm not sure I'm handling the copy constructors properly.

class uid
{
private:
std::string id;
public:
uid(std::string _id)
Do

uid(std::string const & _id) : id(_id) // conversion c-tor
{
id = _id;
Prefer initialisation to assignment. Drop the line above in favor of
using the initialiser list (the stuff after the ':').
if (id.size() & 1) // is the length odd?
id.append(" "); // yes. Now the length should be even
}
uid& operator=(std::string& _id)
uid& operator =(std::string const& _id) // conversion assignment
{
id = _id;
if (id.size() & 1) // is the length odd?
id.append(" ");
return *this;
}
uid& operator=(uid& _id)
uid& operator =(uid const& rhs) // copy assignment
{
id = _id;
Nope. It should be

id = rhs.id;
return *this;
}
~uid(void)
Drop the 'void' (if you decide to keep the d-tor). This is C++, not C.
{
// Nothing to do really.
}
If nothing to do, drop the whole destructor altogether.
};
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '08 #2
On Jun 19, 12:51 pm, SpreadTooThin <bjobrie...@gmail.comwrote:
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 uid:public 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
Jun 27 '08 #3
On Jun 19, 9:51 pm, SpreadTooThin <bjobrie...@gmail.comwrote:
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 uid:public 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:ja*********@gmail.com
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
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Terry Coccoli | last post by:
We have a Siebel implementation and for one query that was taking a long time to run I asked a DBA to evaluate the explain plan. I noticed that he chose to evaluate the Rule based optimization. I...
10
by: lothar | last post by:
for the horizontal rule element, the w3c HTML 4.01 specification http://www.w3.org/TR/html4/cover.html states that the align, noshade, size and width attributes are deprecated. it gives an...
6
by: Chris Travers | last post by:
Hi all; I am using PostgreSQL 7.4 on RedHat 9, though I don't think that is important to this problem. I am attempting to write a rule that will check to see whether certain conditions are...
2
by: Joey P | last post by:
Hi all, I am doing a project for university whereby i have to implement a simple database related to a frozen foods company. I am having some trouble though creating a validation rule for one...
3
by: Yahoo | last post by:
GO TO http://sourceforge.net/projects/sdsre/ TO GET THE LATEST VERSION!!! SRE (Simple Rule Engine) is a lightweight forward chaining inference rule engine for .NET. Its 'simple' because of the...
4
by: Chris Kratz | last post by:
Hello all, We have run into what appears to be a problem with rules and subselects in postgres 7.4.1. We have boiled it down to the following test case. If anyone has any thoughts as to why...
1
by: Prabu Subroto | last post by:
Dear my friends... I want to drop a rule but I get an error message. Could you tell me my I can not drop (delete) the rule? Thank you very much in advance. ps: Here is my try underbelow: ...
1
by: Net Virtual Mailing Lists | last post by:
Hello, I have a table with a rule that goes something like this: CREATE OR REPLACE RULE sometable_delete ON DELETE DO delete FROM cache WHERE tablename='sometable'; CREATE OR REPLACE RULE...
8
by: markjerz | last post by:
Hi, I basically have two tables with the same structure. One is an archive of the other (backup). I want to essentially insert the data in to the other. I use: INSERT INTO table ( column,...
1
by: MLH | last post by:
Anyone remember if A97 append query failure would ever report data breaking validation rule when such was not the case. I have an old SQL statement - several years old now. I've encountered a case...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.