Writing an own string class

T

Tony Winslow

Hi, there!

Some users of this group mentioned before that
being able to write one's own string class in
C++ is a sign of mastering the basics of the
language. My problem is that I don't quite
understand how does that work. I mean writing a
string class is not very hard, but how can I
determine the string class is working properly
enough or even efficient enough? What is the
benchmark thing that I can use?

Tony Winslow
 
E

Erik Wikström

Hi, there!

Some users of this group mentioned before that
being able to write one's own string class in
C++ is a sign of mastering the basics of the
language. My problem is that I don't quite
understand how does that work. I mean writing a
string class is not very hard, but how can I
determine the string class is working properly
enough or even efficient enough? What is the
benchmark thing that I can use?

The requirements on strings are not universally agreed upon, different
people have different requirements. If you can not come up with the
requirements yourself you can look at some existing implementations
(like std::string, QString, etc.). The same goes for performance, find
an implementation with similar functionality to yours and devise some
benchmarks to compare them, or look up some existing benchmarks.
 
O

osmium

Tony Winslow said:
Some users of this group mentioned before that
being able to write one's own string class in
C++ is a sign of mastering the basics of the
language. My problem is that I don't quite
understand how does that work. I mean writing a
string class is not very hard, but how can I
determine the string class is working properly
enough or even efficient enough? What is the
benchmark thing that I can use?

You write your own tests as you go along, starting with the fundamentals and
eventually testing for all the corner cases you can think of. Then release
it to the wild Off the top of my head, and probably with errors something
like this:

Your string class is String. '+' is concatenate. Include a show() function
in as a test device.

String S s("abc");
s.show();
----
String S t();
t.show();
----
modifies x
void f(&S x) { }
....
String S u("abc");
u.show();
----
String S d("abc", e("def");
d = e + d;
d.show();
---
String S a("abc"), b, c;

c = b= a;
____
Something with arrays of strings. Vectors of strings. Prove it is STL
friendly by sorting your strings. And so on.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top