 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
OlgaM Guest
|
Posted: Thu Sep 18, 2003 10:00 am Post subject: problem in constructors in implementing String ADT class |
|
|
I am trying to write specs for string.h class below:
#include <new>
#include <stdexcept>
using namespace std;
class String
{
public:
// Constructors
String ( int numChars = 0 )
throw ( bad_alloc ); // Create an empty string
String ( const char *charSeq )
throw ( bad_alloc ); // Initialize using char*
// Destructor
~String ();
private:
// Data members
int bufferSize; // Size of the string buffer
char *buffer; // String buffer containing a null-terminated
// sequence of characters
};
In this function:
String ( const char *charSeq ) throw ( bad_alloc )
I can't figure out how to set make a new array, dynamically allocate
size and assign charSeq to buffer??
I tried
buffer=new char charSeq
and many other combinations, but nothing works.
Please help!!!!
Olga Mednik
[email]olgamednik (AT) yahoo (DOT) com[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Fri Sep 19, 2003 10:10 am Post subject: Re: problem in constructors in implementing String ADT class |
|
|
In article <37571fce.0309171908.24a8eac7 (AT) posting (DOT) google.com>, OlgaM
<olgamednik (AT) yahoo (DOT) com> writes
| Quote: | I can't figure out how to set make a new array, dynamically allocate
size and assign charSeq to buffer??
I tried
buffer=new char charSeq
and many other combinations, but nothing works.
Putting aside the question as to why you are writing your own String |
type when there is a perfectly good one for use in the Standard Library
(experienced programmers often have need to replace that because of
specific design requirements) all you need is the correct syntax (and a
frightful amount of extra work and understanding to complete the task
you have started)
buffer = new char[std::strlen(charSeq) + 1];
BTW, get rid of the throw(bad_alloc); expert opinion is that it will do
nothing helpful.
--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Richard Smith Guest
|
Posted: Fri Sep 19, 2003 10:19 am Post subject: Re: problem in constructors in implementing String ADT class |
|
|
OlgaM wrote:
| Quote: | I am trying to write specs for string.h class below:
|
First of all, why are you doing this? C++ already has a
fully-featured (some might say overly-featured ) string
class -- it's called std::string. Use it. The chances are
it both more efficient and more correct than your class.
| Quote: | #include <stdexcept
|
This header isn't needed by any of the code you have posted.
It contains std::logic_error, std::runtime_error and their
subclasses. std::bad_exception resides in
| Quote: | using namespace std;
|
Doing this is a header is bad practice. You may be happy to
inject all of namespace std into the global namespace, but
will all the users of your class think the same?
| Quote: | class String
{
public:
// Constructors
String ( int numChars = 0 )
|
This constructor should be explicit in order to prevent
implicit conversions from int to String. For example, would
be happy with the following code compiling?
String("Hello world") == 42;
Second, wouldn't it be preferable to also take the character
to fill the string with? (Presumably you're using the ' '
character at the moment.) I.e.
String( int numChars = 0, char fillChar = ' ' );
| Quote: | throw ( bad_alloc ); // Create an empty string
|
I would avoid using exception specifications (except
possibly the empty one) unless you have a very*good
reason not to. For a good discussion on this suject, read
Item 14 from Scott Meyers' "More Effective C++".
| Quote: | String ( const char *charSeq )
|
Also should be explicit.
| Quote: | throw ( bad_alloc ); // Initialize using char*
|
Again, I would remove this exception specification.
| Quote: | In this function:
String ( const char *charSeq ) throw ( bad_alloc )
I can't figure out how to set make a new array, dynamically allocate
size and assign charSeq to buffer??
I tried
buffer=new char charSeq
|
How about the following?
bufferSize = std::strlen( charSeq ) + 1;
buffer = new char[bufferSize];
std::strncpy( buffer, charSeq, bufferSize );
You need to add 1 to the value returned by std::strlen to
accomodate the ' ' termination. The buffer returned by the
new[] expression is not initialised (it will contain random
garbage), and the the last line overwrites the contents of
the buffer with the string you are initialising with.
Be careful, however, to make sure the code is exception
safe. If, in the constructor, new[] succeeds, and a later
call throws an exception, the destructor will not be called,
and the memory allocated by new[] will be leaked. If the
body of the constructor just contains the above code, you
will be safe as std::strncpy never throws, but if you do
anything else, you will have to be careful.
--
Richard Smith
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Hyman Rosen Guest
|
Posted: Fri Sep 19, 2003 11:03 am Post subject: Re: problem in constructors in implementing String ADT class |
|
|
OlgaM wrote:
| Quote: | I tried
buffer=new char charSeq
and many other combinations, but nothing works.
|
"There is no try, only do!"
Is this case, what you're lacking is proper books or teachers
to instruct you as to how the language works.
Given that this is C++, it's more than likely that with enough
trying you will stumble upon a code sequence that will appear
to be behaving properly but will actually contain undefined
behavior leading to a lurking bug which will manifest at a most
inopportune time.
Here's a working version anyway:
#include <algorithm>
...
String::String(const char *charSeq)
: bufferSize(strlen(charSeq) + 1)
, buffer(new char[bufferSize])
{
copy(charSeq, charSeq + bufferSize, buffer);
}
String::~String()
{
delete[] buffer;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Attila Feher Guest
|
Posted: Fri Sep 19, 2003 11:10 am Post subject: Re: problem in constructors in implementing String ADT class |
|
|
OlgaM wrote:
| Quote: | I am trying to write specs for string.h class below:
[SNIP]
In this function:
String ( const char *charSeq ) throw ( bad_alloc )
I can't figure out how to set make a new array, dynamically allocate
size and assign charSeq to buffer??
I tried
buffer=new char charSeq
and many other combinations, but nothing works.
|
Assuming your string class will and only want to handle nul terminated
strings (otherwise you are missing an optinal length argument).
You have to first count the characters of the C-style string you get as an
argument (strlen from the string library), then allocate that much plus 1
(for the nul) characters long array, and then copy the C-style string there
(strcpy).
Something along the lines of:
buffer = new char[strlen(charSeq)+1];
strcpy(buffer, charSeq);
The thing you have tried above is first of all a syntax error. You could
have written it as:
new char(charSeq)
but this one would try to allocate *one* character and then initialize it
from a character *pointer*, which of course make no sense.
--
Attila aka WW
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|