Global Variables in C++... not as easy as it seems

preview_player
Показать описание


🖥️ C++ Series videos mentioned:

💰 Links to stuff I use:

This video is sponsored by Brilliant.
Рекомендации по теме
Комментарии
Автор

Hope you guys enjoyed this video! If you have any more C++ topics you'd like me to cover, let me know! ❤

TheCherno
Автор

There is also one big problem with keyword "static". Since we have 2 or more copies, modifying one will not modify the other copies

avtem
Автор

Dear Yan, as a 45 year old web developer, it's always a pleasure to watch you and your lectures. I watched your C++ series for elementary information to be able to develop games on Unreal Engine. I watch every video you post, even if I don't understand the content. Especially such concept videos keep our computer engineering knowledge alive, no matter what field you are programming. You are great! Good health to your work and your mind my dear teacher!

bulentgercek
Автор

This is the kind of tutorials C++, and programming languages in general, lack and need! Project structure and technique is so important, but rarely ever taught..

Thanks cherno!

sven--
Автор

Please continue the C++ series, like advance concept introduced in C++20 and maybe it has upgraded further more, multithreading, design concept, UI using c++, Microservice using C++ or maybe webApp using C++. Thanks

ChandraShekhar-bycd
Автор

Hey Cherno was browsing youtube a bit over lunch break and stumbled upon your channel. I was like damn why do i recognize this guys voice so much. Turns out i was following your videos of 3d game development religiously 11 years ago trying to get in to programming! Good to see you’re still going! Since then ive got a bachelors and a masters degree in software engineering! You played a big part of me getting into programming as a kid! Keep going❤

SwnssoN
Автор

“static” having a completely different meaning outside of classes is really confusing. Some people hate it so much they make an anonymous namespace around everything they want to stay internal. Since c++ 11 it does the exact same thing as ‘static’.

marshallsweatherhiking
Автор

Okay i didn´t knew that if you specify a variable as "inline" inside a namespace, that it will only be created once. That is good know, because in the past, i always had trouble fighting with the "static" keyword in C++. So thanks for sharing! But nowadays i mostly use C99 and when i need global variables, i simply have them in one translation unit only, with the ability to make them "extern" if needed. All my libraries that i am writing are single-header-file style with full control how something gets compiled or linked, so by default it uses "extern" but can be overriden to "static" to make everything private in one translation unit only. Also i allow the creation and usage of libraries as well, so i always have dllexport and dllimport in mind as well.

Fnalspace
Автор

Short and sweet, packed with 'Food for though', I love this guy! Great teacher!, thank you Cherno!

jonphillips
Автор

This is my favorite youtube channel. Thanks, Cherno, you're awesome!

miladhakimi
Автор

Have you done a video specifically on the different implications and use cases for inline? It's probably in your book of cpp stuff to cover lol

Love your work, style and flow. Your videos are quick but not rushed, I really appreciate the pace. Thorough is best, luvs it.

paxdriver
Автор

Great videos. There are not very many C++ video developers out there, and your videos are direct and informative. Not like some of these other guys that put a bunch of flashy BS in their videos that just end up being distracting. We really appreciate your work. BTW, I would also recommend using const or constexpr when defining global variables that will never change.

robertmoats
Автор

Great video, I use the separate .cpp file with extern a lot. But I program a lot of embedded, so most objects I create need to be at a specific place in memory anyway. So it makes sense that an interface for example "owns" any registers that belong to it. Something like mathematical constants is placed in the main.cpp file anything module related would be placed in the header as you have shown in the video.

MrHaggyy
Автор

I just love this channel and your teaching methods!

rcookie
Автор

I love this kind of video. Can't explain how much I learn by watching your videos. Thanks alot, Cherno.

Fernando-duuj
Автор

Definitely wanna see a video all about the inline keyword :D Or does that exist already?

gameofpj
Автор

It's great how you explained that compiler error that jumps out when you forget the extern keyword or leave variable definition (not a declaration) in a header file. It was intimidating when I was starting the adventure ;) Now I'd say for a global variable I would either use extern, or... avoid it. Instead of using global variables in namespaces you can use static class members, then the class becomes the namespace for the variable. The syntax for the definitions is a little clunky, but I place all of those in a foldable region to get rid of them from my view. Another way to have some global variables is to put them into one struct, that then can reside either on the main stack frame or on the heap. I tested various approaches, like namespaces vs "static member only classes" and the classes won.

Автор

I started watching your C++ series when I was in college. Today I am a web developer and not written C++ from years. But I still like watching your videos and it teaches me how to do good programming.
Your videos are fun.
Keep it up Guy.

jptiynn
Автор

This was a beautiful video! Explained the topic, issues and solution very clear. Love it. Great job Cherno! 🦾🤖

joseortiz_io
Автор

I didn't know about "inline" keyword for global vars. Thanks for new knowledge 😉

cheerwizard