Mixing literal text and variables values inside a PrintLine

Hi, I’m new to C++, and this might sound like a very simple question but,
In lesson 51 “Coding The Basic Game Loop”, I made the Printline to show the HiddenWord variable value like this:

PrintLine(TEXT("You have Lost!, the word was ") + HiddenWord);

However, for what I learned so far in the course I would have expected it to work with the Insertion Operator “<<” like this:

PrintLine(TEXT("You have Lost!, the word was ") << HiddenWord);

This doesn’t work, but I don’t understand why.
If anyone can give me a hint of what should be looking for so I could read and learn more about this it will be appreciated.
Thanks!

The insertion operator is only for the iostream library and not something part of the C++ language. << is actually the bitshift left operator that iostreams have overloaded to do something different (insertion).

So an example of what << normally does

int value = 3 << 2;

Would mean value is 12, as the bit representation of were shifted over by two to the left.

0011 - 3 in binary
1100 - 12 in binary

As this is binary, X << N effectively means times X by 2 to the N. (3 * 4 earlier)
So

"Hello" << SomeString

Doesn’t mean anything logical. As you’re trying to shift the bits of a string literal (which you can’t do) by an FString which doesn’t make sense.

I wasn’t aware of this, there are many things happening around the code we are writing and sometimes get’s confusing, now it starts to look more clear, thank you so much for the explanation!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms