Sign in to follow this  
AndiDog

String concatenation operator

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
@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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this