Skip to main content
gamedev.net
🔒 Locked

overloading operator= ?

Started by bilsa Jan 19, 2004 at 2:54 PM 1 replies 250+ views
Original Post
bilsa
bilsa
Hey, I''m making an ERROR class. And I would want calls like this be possible:
//I would want this syntax to work, but I''m getting errors:

ERROR result = someFunc(...);

//this doesn''t work

//Error: Cannot convert from char[x] to ERROR&

ERROR& someFunc(...) {
return "TEST_ERROR"
}

//ERROR is a class

class ERROR {
public:
char* err_message;
char* err_message_detailed;
ERROR& operator=(const char* error_UserId) {
...
//I seach through a map for error_UserId,

//and result gets a pointer to an ERROR object

ERROR* result = findError(error_UserId);

//Then I assign this objects variables to the

//ones that the ERROR object corresponding to

//error_UserId

err_message = result->err_message;
err_message_detailed = result->err_message_detailed;

//then I return the new object...

return *result;

}
//...

}
Do you guys get what I mean? So far I have managed this syntax to be possible:
//this works fine...

ERROR result;
result = "TEST_ERROR";

//But the one in the source block above with the someFunc 

//doesn''t work.

//I believe it has something to do with that ERROR result

//cannot be initialized to a char* type. But even if I am right

//I still don''t know how to make my goal possible

Thx, appreciate it
Sneftel
Sneftel
Assignment within a declaration isn''t actually assignment. It''s construction. In other words, while MyClass a; a = 3; will invoke the assignment operator, MyClass a = 3; will not. Instead, it''ll look for a non-explicit constructor which takes one argument, of a type which the right-hand-side of the ''assignment'' may be converted to.


class MyClass
{
public:
MyClass(int i) { ... }
};


Like that.



"Sneftel is correct, if rather vulgar." --Flarelocke
bilsa
bilsa
Thank you very much Sneftel!

Topic Locked

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

Sign in to reply to this topic.