From the course: C++ Best Practices for Developers
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Auto type deduction and initialization - C++ Tutorial
From the course: C++ Best Practices for Developers
Auto type deduction and initialization
- [Instructor] The keyword auto tells a compiler to deduce the type of variable by the expression used to initialize it. Unlike other types, auto requires the variable to be initialized, so there's no chance of a variable running around uninitialized, preventing a difficult-to-find bug. Auto also helps us to write our code to interfaces and not to implementations, avoiding tight coupling. If you need a specific type, you can use auto with type initialization. Let's take a look at a few examples. Let's try to create a vector event. First, we initialize alpha to a vector event with the values one, two, and three. We try to do something similar with auto but we get a syntax error. Since initialization of auto expects one expression, not three. We then try to declare gamma with auto and an initializer list. The compiler is okay with it, but we created a variable of type initialize list event, not vector event. Then we defined delta with a vector event and an initialize list, that works…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
(Locked)
Auto type deduction and initialization1m 27s
-
(Locked)
Range-based for loops1m 45s
-
(Locked)
Strongly typed enums1m 58s
-
(Locked)
Using lambdas2m 39s
-
(Locked)
The magic of variadic template functions2m 12s
-
(Locked)
Challenge: Writing an integer sum method21s
-
(Locked)
Solution: Writing an integer sum method2m
-
(Locked)
-
-
-