253: Creative Ways To Use C++ Curly Braces Beyond Just Functions And Loops.

Take Up Code - Podcast autorstwa Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes

Kategorie:

This episode will explain how you can use curly braces in C++ to create a new scope. You can use this ability to control name visibility and reduce name conflicts. And you can also use curly braces to control exactly when object constructors and destructors are run. This is possible because C++ has very specific rules about when objects are constructed and destructed. Other languages that use garbage collection can’t give you this level of control. If you’d like to improve your coding skills, then browse the recommended books and resources at the Resources page. You can find all my favorite books and resources at this page to help you create better software designs. Listen to the full episode for more details or read the full transcript below. Transcript Did you know that you can use curly braces for more than just function and loop bodies? When writing a function, or a class, or an if statement, or a loop, C++ uses an opening curly brace to begin the body of the function, class, if/else statement, or loop. Then you put all the normal statements and close it all with a matching closing curly brace. We get so used to this that we don’t usually notice the curly braces anymore. At least until we miss one and have to hunt through the code to find where it’s missing. It helps to keep your braces aligned and your indentation consistent. It makes it easy to spot a missing curly brace. This is one of the reasons that I put curly braces on their own lines. I like mine unindented from the class, method, or loop and then indent the code inside them. That’s just my personal preference. A lot of books place the opening curly brace at the end of the previous line which saves vertical space. This is important in books. And I think a lot of people become used to see code written this way and then write their own code the same. This episode is not about where to put your curly braces though. The only thing I’ll suggest is that whatever style you like, at least be consistent. This episode is about something else. Something so obvious that many people are not even aware of it. You see, not only are braces used to define the body of a class, method, or loop, they also define a scope. If you declare a local variable inside a method, then that variable is scoped to that method. This means the compiler will only let you refer to the variable by name from within that method. When the method begins, the variable is constructed. And when the method ends, the variable is destructed. And you can do the same thing inside a loop. Only this time, the variable will be constructed and destructed on each pass through the loop. And you can declare member variables inside a class definition which then get constructed whenever you create instances of the class and get destructed when the class instances get destructed. The curly braces define the scope and how they’re used, either as part of a class, or a method, or a loop defines the lifetime of that scope. The interesting thing is that you don’t need to write a new method or loop in order to get a new scope. As long as you’re writing code inside a method, you can put matching opening and closing curly braces anywhere you want. They don’t have to be attached to an “if” statement or a “for” loop. The real question is why would you want to do this? The C++ language is very specific about when and in what order constructors and destructors will be called. When you need to adjust this order, then adding a new pair of curly braces might be just what you need. The nice thing is that you’re not limited to using only the variables declared inside the curly braces. You just need to be aware that any variables you declare inside the curly braces won’t be available anymore once you leave. Let’s say you declare an int type called count at the beginning of a method. You can use count from that point forward until the end of

Visit the podcast's native language site