Skip to main content
gamedev.net
🔒 Locked

String concatenation operator

Started by AndiDog Mar 20, 2007 at 2:59 PM 9 replies 1.6k views
Original Post
AndiDog
AndiDog
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?
dave
dave
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.
AndiDog
AndiDog
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)
Omid Ghavami
Omid Ghavami
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
AndiDog
AndiDog
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 :)
TheUnbeliever
TheUnbeliever
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]
rip-off
rip-off
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.
AndiDog
AndiDog
@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
Enigma
Enigma
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
AndiDog
AndiDog
Of course it's 4 learning only, as always...
Zahlman
Zahlman
Quote:
Original post by Enigma
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 here's a simple illustration of why the language disallows it:

class MyString;class YourString;MyString operator+(const MyString&, const MyString&);MyString::Mystring(const char*);YourString operator+(const YourString&, const YourString&);YourString::YourString(const char*);"foo" + "bar"; // even if the compiler were allowed to do the implicit conversions// and then select the appropriate operator+ - it has no way to figure out *which*// to use.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.