0

For some reason the structure window declared and used inside of the curly bracers, why does author do that?

// separated header with defined struct
typedef struct SIZE
    {
        UINT Width;
        UINT Height;
    } SIZE;

.....

// .cpp file that uses header with SIZE but doesn't have a straightforward relation to header
void foo(UINT W, UINT H)
{
    // magic
    {
        SIZE window = {};
        window.Width = W;
        window.Height= H;
        // magic with window
    }
    // magic
}
clearlight
  • 12,255
  • 11
  • 57
  • 75
kLateralus
  • 27
  • 8
  • 2
    Curly braces define an object's scope. Any object (such as `SIZE window`) will go out of scope when the program exits the curly braces it was declared in. You won't be able to access `window` in any of the `// magic` areas you have marked. – JohnFilleau Feb 20 '20 at 20:30
  • 1
    If you can't understand the tutorial, is it really a good one? And if it is, how would you know? – user4581301 Feb 20 '20 at 20:30
  • 2
    A funny thing about tutorials is that if you need them you can't tell how good they are, and if you can tell how good they are you don't need them. Case in point: this looks like C, which is pretty awful for a C++ tutorial. (There are no known good C++ tutorials online. Get yourself [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list).) – molbdnilo Feb 20 '20 at 20:41
  • Guys thank you for all your answers, big thanks for telling me your concerns about the proper usage of the struct definition in c++. I have to tell that the struct was defined in another header that's not a part of the tutorial but in use for the case. And another huge thank you to @John for explaining to me the purpose of curly braces :) – kLateralus Feb 20 '20 at 20:44
  • Can you be more specific about what you don't understand? – Jim Rhodes Feb 20 '20 at 20:44

1 Answers1

1

In

void foo(UINT W, UINT H)
{
    // magic
    {
        SIZE window = {};
        window.Width = W;
        window.Height= H;
    }
    // magic
}

The extra curly braces establish a compound statement, also known as a block. In this example, this block doesn't do anything except establish the scope of window. window only exists within the block; as soon as the closing brace is reached, window will be destroyed.

Side note:

The initialization of window can be greatly simplified:

void foo(UINT W, UINT H)
{
    // magic
    {
        SIZE window = {W,H};
    }
    // magic
}

This makes use of Aggregate Initialization.

Another note: Avoid using ALLCAPS identifiers for anything but constants. It can lead to unexpected macro substitutions with a previously defined constant just like this poor soul found out the hard way. In addition, single letter variables should be avoided. A single letter tends to not be descriptive enough to convey what is represented by the variable and they are very easy to accidentally mix up while also being very hard to spot when mixed up.

user4581301
  • 33,082
  • 7
  • 33
  • 54