I am creating my own class for String
using C++ solely for learning purposes.
And I stuck upon the place where I should make a decision. Let me explain the matter.
I have two options of my class. I will post below only relevant pieces of the code because I do not want to distract you from my problem at hand. If in order to help me out you need more info I will gladly provide it.
Option 1
class String {
size_t _length;
char* _stringHead;
public:
String(const std::string&);
String(const char*);
String(const char);
};
String operator+(String, const String);
const bool operator==(const String, const String);
const bool operator!=(const String, const String);
const bool operator<(const String, const String);
const bool operator<=(const String, const String);
const bool operator>(const String, const String);
const bool operator>=(const String, const String);
Option 2
class String {
size_t _length;
char* _stringHead;
public:
//irrelevant part of code in Option 2
String(const std::string&);
String(const char*);
String(const char);
//irrelevant part of code in Option 2
};
String operator+(String, const String&);
const bool operator==(const String&, const String&);
const bool operator!=(const String&, const String&);
const bool operator<(const String&, const String&);
const bool operator<=(const String&, const String&);
const bool operator>(const String&, const String&);
const bool operator>=(const String&, const String&);
//for std::string
String operator+(String, const std::string&);
const bool operator==(const String&, const std::string&);
const bool operator!=(const String&, const std::string&);
const bool operator<(const String&, const std::string&);
const bool operator<=(const String&, const std::string&);
const bool operator>(const String&, const std::string&);
const bool operator>=(const String&, const std::string&);
String operator+(const std::string&, String);
const bool operator==(const std::string&, const String&);
const bool operator!=(const std::string&, const String&);
const bool operator<(const std::string&, const String&);
const bool operator<=(const std::string&, const String&);
const bool operator>(const std::string&, const String&);
const bool operator>=(const std::string&, const String&);
//for std::string
//the same goes for char* and char
...
//the same goes for char* and char
So, as you can see from the Option 1 and Option 2 specs the decision here is about whether to use implicit type conversion which is done with the help of constructors or to type each utility separately for each type with which I want my String type to work.
As far as I can see right now the benefit of using the first approach is that it is easier to implement and maintain. While the second approach may produce better performance results.
I would like to get constructive arguments which approach is better and in what scenarios and which approach hence would you use. I think that the biggest part I am interested here is whether or not the performance benefit of the second approach is reasonable.
const bool operator==(const String, const String);
creates copies of theString
parameters which are of arbitrary length. Seestd::string::operator+
for comparison: en.cppreference.com/w/cpp/string/basic_string/operator%2B – Tantalizeexplicit
. – Chachastd::string
toString
. Did you mean to remove the conversion from Option 2? – Noted