0

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;
1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
Makogan
  • 8,208
  • 7
  • 44
  • 112
  • Sounds like an XY problem - I cannot believe there is any reason for attempting to do this. –  Jul 13 '18 at 22:46
  • 1
    Is this Q&A any help? [How do conversion operators work in C++?](https://stackoverflow.com/questions/1307876/how-do-conversion-operators-work-in-c) – user4581301 Jul 13 '18 at 22:47
  • Also if you compiler is up to date with the C++17 standard you should take a look at `std::variant`. – user4581301 Jul 13 '18 at 22:48
  • @NeilButterworth How is this an XY problem? All I want is to be able to assign to a string the value stored in the union. Seems pretty simple conceptually. – Makogan Jul 13 '18 at 22:49
  • Why do you want to use a union at all? –  Jul 13 '18 at 22:50
  • @NeilButterworth Because the data structure needs to be passed to the GPU with a specific alignment. The needed alignment needs a structure with 8 bytes (pointer/ long). The structure must for half of the time have a string and half of the time have a long as it;s identifier. The only solutions I can think off without unions involve duplicated code and copying data. Copying data is particularily problematic because this is a real time system. – Makogan Jul 13 '18 at 22:55
  • Neil is edging around a valid concern. What you have here is a very effective memory leak factory. Both `operator=` need some extra logic to clean up any existing `std::strings` that are being pointed at and then further logic is needed to be Rule of Three/Five compliant. – user4581301 Jul 13 '18 at 22:56
  • I was aware of the extra code needed to get rid of the memory leak. Although I have no idea what the rule of three/five is – Makogan Jul 13 '18 at 22:57
  • "Although I have no idea what the rule of three/five is " - if you are programming in C++, find out before you go any further. –  Jul 13 '18 at 22:58
  • Rule of Three: If you need an assignment operator, copy constructor, or destructor, you almost certainly need all three. More complete run-down: https://en.cppreference.com/w/cpp/language/rule_of_three You cannot write valid, non-trivial C++ programs without a good understanding off Zero/Three/Five. – user4581301 Jul 13 '18 at 22:59
  • How is your GPU going to know what a C++ std::string is? –  Jul 13 '18 at 22:59
  • @NeilButterworth it won't. The string value will be replaced by a long. Identifying the offset of a texture in a sampler array. – Makogan Jul 13 '18 at 23:00
  • Could you not have a string table that mirrors the look-up you will do with the `long`? Might save you a lot of trouble. – user4581301 Jul 13 '18 at 23:01
  • Technically yes, as a matter of fact that's more or less how it works already. The issue is, while loading an mtl and obj file's information, I need to keep the string information in order to associate to each mesh object the material objects it uses. Once everything has been loaded however, I need to put the materials into a texture array in the GPU. So as I load each material's texture file I replace the string value with the position in the array it was loaded into. – Makogan Jul 13 '18 at 23:04
  • 1
    1201ProgramAlarm's posted as good an answer for the actual question as I think you can get, and the comments are wandering off topic for Stack Overflow. Once you have this all sorted out and working, I recommend making a little sample program demonstrating `TextureID` and how you use it and post over at Code Review to see what improvements/alternatives are out there. – user4581301 Jul 13 '18 at 23:30

1 Answers1

3

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
}
1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56