String concatenation operator

Started by
9 comments, last by Zahlman 17 years, 1 month ago
My simplified string class (only constructors and operators):
class String
{
    String();
    String(const String& s);
    String(const char *s);
    ~String();

    operator char*();

    String& operator = (const char *s);
    String& operator = (const String& s);
    
    String operator + (const char *s);
    String operator + (const String& s);

    String& operator += (const char *s);
    String& operator += (const String& s);
};
How can I create a String out of two C strings, like so:
// The following operator does not work, GCC says
// invalid operands of types `const char[5]' and `const char[10]' to binary `operator&'
String operator & (const char* a, const String& b) { /* concatenate them here */ }

int main()
{
    String str = "test" & "something";
}
Which operator do I have to implement?
Advertisement
Let me ask you this, what happens if you want to add the two values of the pointers together?

Overloading an operator for this might not be a good idea.
That's why I'm using & instead of +.

Anyhow, what if I just wanted to know *how* to do it?? In case it's bullsh**, I'ma find that out myself ;)


EDIT: Adding pointers (char *a, *b, *ptr=a+b) <--> adding strings (char *a,*b;String s=a+b)
Quote:
// The following operator does not work, GCC says
// invalid operands of types `const char[5]' and `const char[10]' to binary `operator&'


Do you expect the string "something" to magically turn in to an object of type String?

EDIT: Operator overloads which might lead to confusion are not recommended. You should instead have a class method for concatenation, for example.
Best regards, Omid
I wondered if it's possible that the compiler uses the constructor String(const char*) to create a String class out of "test".

Guess your irony says "no" to my assumptions :)
Overload the + operator and then use temporaries.

String str = String("Foo") + String("Bar");


EDIT: Of course it can't do that - think about it: how does the compiler know that that particular constructor doesn't just take the char array and use it to name itself or something like that?
[TheUnbeliever]
The compiler explains it.

A string literal (anything in quotes) has a type of const char[num_characters].

Your code attempts to use the "&" operator on them, which is not valid.

Now, if you were to explicitly make one of them a String:

int main(){   String test = "test"   String str = test & "something";   // or   String str = String("test") & "something";}


Quote:
That's why I'm using & instead of +.

What does String::operator+() do then?

Finally, operator char *() is dangerous. At the very least make it const, but consider not using such an implicit operator anyway. They can lead to confusing errors where the compiler doesn't do what you expect. It is not without reason that the standard c++ string uses a member function called c_str() over an implicit c-string operator.
@rip-off: The & was just in my example, I'm actually using + in my code.

Yes right, but temporaries did already work in my class, thanks.

But since it's the only way...

Thx a lot guys
Since it doesn't appear to have been explicitly stated yet, any operator overload you define must have at least one parameter of user-defined type. You cannot redefine operators for built-in types.

And this is just a learning exercise right? You're not actually thinking of using your own String class in a serious project?

Σnigma
Of course it's 4 learning only, as always...

This topic is closed to new replies.

Advertisement