I'm making a game with C++ and SDL2. I have a class called "text" and a class called "buttonObj"
The constructor of "text" is a void function that takes an SDL_Color variable.
The first part of buttonObj looks like this:
string textContent;
text buttonText;
text highlighted_buttonText;
//Constructor
buttonObj(string tex, SDL_Color notHighlighted, SDL_Color highlighted) :
textContent( tex ),
buttonBase( img ),
buttonText( notHighlighted ),
highlighted_buttonBase( hI ),
highlighted_buttonText( highlighted )
{}
Currently, this works, but I don't exactly know WHY it works. With a constructor initializer, the value outside the parenthesis gets assigned to the value within the parenthesis. So, how come an SDL_Color (i.e "notHighlighted") can get assigned to a text variable? (i.e buttonText)
I assume this is because with a class, the variable inside the parenthesis is a parameter of the constructor of that class. So, since the constructor of text takes an SDL_Color variable, the program treats "notHighlighted" as a parameter, rather than the regular one-line initializer. Right?
Speculation Aside, this works. From my console, I can see two "text" instances are constructed, one for "highlighted" and one for "notHighlighted". Once the texts are constructed, they remain as they should and render as they should later on in the code.
However, this isn't what I want... Instead of passing an SDL_Color into the constructor and having buttonObj turn the color into text, (via constructing it) I want to load some text beforehand (elsewhere during runtime) and pass the text into buttonObj directly. Like so:
string textContent;
text buttonText;
text highlighted_buttonText;
//Constructor
buttonObj(string tex, text notHighlighted, text highlighted) :
textContent( tex ),
buttonBase( img ),
buttonText( notHighlighted ),
highlighted_buttonBase( hI ),
highlighted_buttonText( highlighted )
{}
According to my knowledge of what one line initializers are SUPPOSED to do, "highlighted_buttonText" should become equal to the parameter "highlighted." So "highlighted_buttonText" is set to whatever the parameter "highlighted" is, since they're both of type "text"
However, this doesn't work. In my console, I see that the texts are constructed and then destructed! As if something is calling the destructor... this means that the variables don't work fine later on in the code. (And then I get a bunch of exceptions in the library I'm using for text rendering. It's a text chaching library based on SDL_TTF called SDL_FontCache)
I know this is a hard issue to explain and I might not be describing it well. But please, any advice would be awesome! Weird problems like this irk me...