I have a weird union I am using for alignment reasons.
I have overloaded it so that it can be assigned a string value.
Now I wish to overload the = operator so that I can assign it TO a string value.
union Tag
{
std::string * path;
long id;
};
struct TextureID
{
Tag ID;
int type;
TextureID& operator= (std::string str){ ID.path = new std::string(str); type=0; }
TextureID& operator= (long val){ ID.id = val; type=1; }
};
In this case we have overloaded the operators such that
TextureID t = "hello";
Is a valid statement.
How may I overwrite the = operator to be able to do:
string s = t;
You can create a conversion operator to convert your TextureID
to a std::string
operator std::string() const {
// logic to create a string to return
}
or create an explicit function to do the conversion
std::string to_string() const {
// logic to create a string to return
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.