Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Fernand

overloading operator with strings (C++)

This topic is 5369 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I''m having troubles to overload the operator + to concatenate strings!! I make the program down but there is an error that I can''t resolve!! What can I do to resolve this problem?? class string { private: char str[80]; public: string() {str[0]=''0'';} string(char s[]) {strcpy(str,s);} string operator +=(string s) const//concatenate { string temp; if(strlen(str)+strlen(s.str) < 80) { strcpy(temp.str,str); strcat(temp.str,s.str); } else cout << "\nMemory Full!!"; return temp; } string operator +(string s) const //concatenate { string temp(str); temp+=s.str; return temp; } void print() const { cout << str; } //print }; Thanks!! Fernand!!

Share this post


Link to post
Share on other sites
Advertisement
Here's how you should declare your operators:

const string& operator += (const string& r)
{
// code to concatenate r to 'this' goes here
return *this;
}

put operator + outside of class:

inline string operator + (const string& left, const string& right)
{
string temp(left);
temp += right;
return temp;
}

[edited by - ABC123 on February 8, 2004 3:30:00 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by DerAnged
class string? wouldnt that be a redefinition?





I think no, because at main function I define the strings s1 and s2. Next, I use the comands to concatenate and to attribute the strings s3, s4, s5:
s3 += s2;
s4 = s1 + s2;
s5 = s1 + s1;
There is another way to do this??

Thanks!!

Share this post


Link to post
Share on other sites
quote:
Original post by DerAnged
class string? wouldnt that be a redefinition?






''string'' is only a redefinition if your using namespace std.

You can define your own class called ''string'' as long as your not doing it within the context of namespace std.

Share this post


Link to post
Share on other sites
Please, please, please use functions that check bounds. Using functions like strcpy() and strcat() instead of strncpy() and strncat() are one of the main reasons that buffer exploits exist. Oh, and don''t forget about the terminating null character when you''re calculating lengths and whatnot.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster


#include <string>

int main()
{
std::string s1,s2;

s1 = "hello";
s2 = "world!";

std::string s3 = s1+" ";
s3 += s2;
}


Why are making your own string class?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by ABC123
Here''s how you should declare your operators:

const string& operator += (const string& r)
{
// code to concatenate r to ''this'' goes here
return *this;
}

put operator + outside of class:

inline string operator + (const string& left, const string& right)
{
string temp(left);
temp += right;
return temp;
}

<SPAN CLASS=editedby>[edited by - ABC123 on February 8, 2004 3:30:00 PM]</SPAN>


Hi,

The operator + is function very well, but I''m having troubles with operator +=!! Which is the best code to concatenate r to ''this''??

Thanks!! Fernand!!


Share this post


Link to post
Share on other sites
Concatenating to ''this'' all depends on how you''re implementing your internal buffer to hold the string chars.

If you''re using a dynamic buffer, you may need to grow the buffer by the length of ''r'' before concatenating.

Regardless of your internal buffer, you just append ''r'' to the character buffer of ''this'' and return ''this''.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
std::string already has a += operator that does what you want.

Why are you making your own string class?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!