1 - why i must add the second parameter if it's used automatic? |
It's not used automatically. You simply don't make use of it.
The size parameter can be useful for performance reasons because you don't need to loop through the string to find the size.
If you want to allow strings with null characters ('\0') in them it's actually necessary to make use of the size parameter because strlen and similar methods of detecting the size will stop at the first null character. With you current implementation it looks like "hello\0world"_b; will construct a String object containing the value "hello".
std::string has a constructor that takes the string as first argument and the size as a second argument. You might want to do something similar for your String class.
2 - why i can't add these function inside the class? |
I don't know, but they don't operate on existing objects so usually there would be no
3 - if the:
1 2 3 4
|
operator const char*()
{
return chrString;
}
| |
is 1 way, of some, for use cout... what is the other for use cin? |
That has nothing to do with cout. What it does is that it makes String implicitly convertible to a const char*.
If you want cout to work for your String class I recommend you overload operator<< instead.
The equivalent for cin would be to overload operator>>.
1 2 3 4 5 6 7 8 9 10 11
|
ostream& operator<<(ostream& os, const String& str)
{
// ...
return os;
}
istream& operator>>(istream& is, String& str)
{
// ...
return is;
}
| |
Note that you cannot define these operators as members because the first parameter is not the String object so it might make sense to define them as friends.