-
overloading operators
Code:
class String
{
protected:
std::string string;
public:
String operator= (char *str);
}
String String::operator= (char *str)
{
string = str;
}
I want this to function like the string class that when you create a string you can assign a char pointer to it.
For instance, I want to be able to do:
Code:
String s;
s = "Hello, world!";
However, when I try to run that my program crashes. Is there something I'm doing wrong?
-
you dont get any warnings after compiling? do you have to change the parameter to 'const char *str'? if that doesnt help could you post the complete code so i can try it?
-
Did you #include <string>?
I couldn't even get the code to compile.
Operator= should return a String reference, using the this pointer.
-
I included <string>
Here is everything you should need that I have:
Code:
class String
{
protected:
std::string string;
public:
String operator= (char *str);
};
String String::operator= (char *str)
{
string = str;
}
int main(int argc, char *argv[])
{
String s;
s = "Hello, World!";
return 0;
}
-
your problem is solved by doing what MacNilly suggested stated
-
He said I should include <string> and I did.
"Operator= should return a String reference, using the this pointer."
I'm not entirely sure what that means or what I should do with it.
I just want to be able to set the std::string inside of the String class equal to a value as a regular std::string can. Am I on the right track and if MacNilly told me how to fix it. I don't really understand.
Sorry, but could someone elaborate a little.
Thank you.
-
he said you should return 'this' from your operator= function, just like how you 'return 0' in main, but 'this' instead of '0'. in doing so, you have to change the return type (from String) so that it returns a pointer to a string, so change it to String*.
hopefully you can get it!
-
That works, but I don't understand the logic at all. I would really like to overload a lot of operators in the program I'm working on right now. So, if anyone could explain how this works or direct me to a good article about this that would be great.
I searched google, but I didn't really get anything direct.
-
Code:
your_String = "static string literal";
Is the same as
Code:
your_String.operator=("static string literal");
Now, if you have
Code:
my_String = your_String = "static string literal";
Then you really have
Code:
my_String.copy_constructor(your_String.operator=("static string literal"));
The only argument the default copy constructor takes is a reference to an object of the class's type (String). So, have your operator=() function return a reference to a String. Which String? Well, which object does it make sense to return? this object. So, we return *this (the actual object calling the operator), for which a reference to is returned by the function.
-
Why wrap a class around just a std::string object, when you can just use a std::string object? I really don't see the point!
-
Because I'm using it to render strings. And if I wouldn't have the class you would have to have a function with 4 arguments instead of string.draw(x, y);
-
It still seems a bit dubious that you'd need to reimplement a lot of std::string functionality to render it. Shouldn't you be able to do draw(string, x, y)?
Other than that, is a function with 4 arguments that bad? You could also reduce the number of arguments by packing x and y into a Point struct. (draw(string, place, colour) for example)
-
Right. But then I would have to have the font rendering strings. Instead of the strings rendering themselves. Which wouldn't be so bad if my engine wasn't set up differently. And if I did that I wouldn't be able to build the texture for the string before hand if it was a constant string.