Namespaces in C++

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up guys my name is eterno welcome back to my c++ series so in the last video I explained why I don't like using namespace STD if you guys haven't checked that video out definitely do so but I haven't actually talked about what namespaces are in this simple spot series and because in that video I specifically wanted to talk about the standard namespace and why I don't like using namespace STD but today we're going to be talking about what namespaces are in C++ why they're useful when I would when I wouldn't use them why they exist and all the different things that we can do with them so if we jump right into this this is the code that we had written in that previous video where we talked about why I don't like using namespace everywhere and all that kind of stuff let's talk a little bit about what we actually see on the screen here and how we can further extend our usage of namespaces and why main spaces are actually used unnecessary so over here we essentially have this issue where we have to print functions now they do have different signatures one of these takes in a string one of these takes in a Const are but if we kind of revert to maybe both of them taking in a constant R so if I change this string 1 to actually take in a concert char pointer like so we kind of may have an issue if we weren't using namespaces because these functions are identical if I just kind of comment out this namespace Apple line of code here and the orange one as well suddenly we have a little bit of an issue if I try and I'll fix up this error as well if I try and just compile this by hitting ctrl f7 you can see over here that I get an error because it's telling me that this print function that takes in the constructor already has a body because I have two symbols with the same name and they're identical symbols now when I say symbols I'm talking about things like classes functions variables constant that kind of that kind of thing right so we have in this case two functions called print which take in constructor and return void they have the same signature so these two symbols are identical we can't have two identical symbols that's a linking error and potentially a compilation error in there if they're actually in the same file so how do we fix that what if we do want to have to print functions or what if we use another library that has already defined a print function but we want to have our own print function now back in the days of C and in fact that still is a thing C doesn't have namespaces which is why if you would have noticed in the OpenGL series as an example we're using a library called GL FW which is an OpenGL kind of windowing and context library and in putting all that kind of stuff right but the point is jello W is a C is a C library it's compatible with both C and C++ because it's a library they can't use namespaces which is why if you notice in that series all of the code that we write that has something to do with JFW begins with like literally every function starts with GL FW and then like the function name for example gel W in it or jail of W create window all of that stuff has jelf W literally prepended to the front of it with opengl it's kind of the same thing as well we have GL and then the symbol name so like GL begin GL & GL j and vertex buffers that kind of thing right we have an app deed the actual name of the library or some kind of ID embedded into the function name so in our code that will be the equivalent of if I had literally written something like Apple print right and orange print and then now you can see that I okay these two functions are different because this is like the Apple print version this is the orange print version and that's actually what C libraries tend to do because they can't just you know I can't make a function just called Ahmet because in it is such a common name what if you know if every library was to do it was to just name their functions in it then you could never use those libraries together without having to modify the source code so what they do usually is just kind of prepend the name of library to it so J of W in it and that works because well it's a fairly unique name right delft w in it okay cool that's specific to that G of W library now in C++ instead of us having to do all this we have namespaces to solve that issue the primary purpose of namespaces is to avoid naming conflicts that's why they exist to avoid naming conflicts okay that's it sort of just if you've getting confused by why namespaces exist or why they're being used it's because of naming we want to be able to call symbols the same name in different contexts so if we go back here into Apple print instead of having this what we can do is enclose this symbol in a namespace in this case what we write is namespace the name of the namespace and then just as if it was any kind of function we surround it with curly braces which everything in everything in between these curly brackets is included in that Apple namespace now personally I like to write my curly bracket on the same line as the actual namespace declaration I don't like doing this the reason that happens is because when we have multiple nested namespaces it gets a little bit messy so what I like to do is if we had Apple and then maybe like functions or something you know I have this here and also with the end braces instead of kind of writing it this way which is what Visual Studio will try and try and order formatted to I like to just have it on the same line that way my actual functions are one level indented and not like 2 or 3 or 4 or depending on how many namespaces we actually have nested so that's just my personal preference you don't have to follow that but that's why I kind of put all this stuff on the same line so that being said we now have a have an Apple print function I've grabbed it here into orange and get rid of these comments as well we have this orange print function I can rename it to just print so we're able to have two functions with exactly the same signature in this file and when it comes time to call it what we do is we need to specify which one way we need to call so print is now inside either the apple or the orange namespace we have two of them so if we want to use the one from Apple we just type Apple : : and then print this : : is essentially like a namespace operator so what you do is every kind of namespace that you go as you drill down into a namespace you just put a call and : and that is what I guess enters into that namespace or allows you to call things from that namespace the same thing applies to static functions I guess or symbols in classes and like methods and classes and all that stuff a class is kind of like a namespace it is it is a namespace of its own which is why if we're accessing like an inner class inside the class or like an enum or you know like I gave the example of like static functions or even non static functions in some cases we use : : so that's what the : : kind of means so this double colon is used to get the print function from the apple and if we were to run this we would just get holo printing normally if I want to call the orange one obviously I type in orange now that whole using namespace thing that we talked about in the last episode was just a way to not have to write this kind of apple or orange kind of prefix so instead of having to do this what we can say is that actually we're using namespace Apple which means that basically import everything from the namespace Apple as if I had never specified it as a namespace so in this case print will be used from Apple if I want I can change it to orange I can also say if we have multiple things in here like we have print and maybe print out an O print again or something if I have multiple functions maybe I don't want to use print again from orange I just want to throw that as an apple let's go to using namespace apple maybe I don't want print again when I put this easy name space I don't want to use print again I still want to specify maybe which print again I want I just want print to be important so to do that we can just write using Apple and then the function name print okay that means that we're going to take this from the Apple namespace specifically but for this we'd still have to specify that namespace like that you can also create aliases for namespaces by just typing in namespace you know a equals Apple or something like that that means I can type in a print like that instead of having to spell out Apple that's very useful for when you have nested namespaces so we can also nest and namespaces as you probably saw earlier so we can have Apple and maybe we include all about function and we can close all about functions inside the dysfunctions namespace for some reason you can ask me why just an example so we have namespace apple we have nowadays functions and then instead of us having to write you know Apple functions like that or you know something like using using namespace Apple function like this we could also by the way for this example you can also split this up so you can be like using access apple and using namespace functions which would allow us to not have to specify any namespace for print at all so instead of us having to do this or in the other example that we can also just say namespace a equals Apple functions which means that I can just type in a print as if it wasn't one namespace and I would get that result over there okay so that's a pretty basic example of town namespaces work I'm not sure if there's really anything else that I need to talk about obviously things like things like this are confined to the actual scope so in other words a this namespace a equals Apple functions only exists within this main function I can't access to the outside my recommendation is when you do things like this like namespace a equals blah blah or using namespace whatever my recommendation is you try and confine it to a smaller scope as possible if you just need it inside an if statement for example you know right you write your declaration inside that if statement if you just need it inside a function then writing inside the function and then as a last result as a last resort write it in there like kind of in a top-level file but don't try and like just you know declare it in at the top level at all times or in header files certainly never do it in header files the reason we don't want to do that is because when we get you know if we start using namespace everywhere then as you can probably imagine that kind of undoes all of the helpful work that namespaces do by avoiding naming conflicts because if we're using it what's the point of having our functions in namespaces if we have to if we if we use namespace anyway right it imports everything which means that we might as well not be using namespaces and we can get back to the problem of having naming conflicts between libraries or between our and code so that's just a few things to think about I've left a link below to the cpp reference page on namespaces this has a few examples of what you can actually do so there's also something called an inline namespace really just kind of makes everything available to the parent namespace we also have since it was lost 17 the ability to actually specify namespaces kind of like this so in this case when I had this functions instead of having to do namespace Apple then namespace functions I can actually just write namespace functions like that be a way that that's not supported by all compilers of course and it is a new kind of C++ thing I recommend you read through this page if you want to know everything that you can do with namespaces I've pretty much covered most of the useful things obviously the reason why we use namespaces is kind of - if we are creating a library of code or if we just have our project we want to put it behind a namespace so that we don't get any naming conflicts that way I'm free to create any function that I really want everything in the C++ standard library is behind the standard STD namespace so obviously you're not going to get naming conflicts like that however if you use any kind of C code no such thing as namespaces which means that when you do use C code you are potentially risking a naming conflict so even in your C++ project I would still use a namespace of course because if you use any C code then that's going to be an immediate conflict if it has the same function name as well you're actually declaring anyway that's namespaces leave your thoughts below leave your thoughts on why you use namespaces why you don't like using main spaces where you would where you wouldn't all that kind of stuff I think it's pretty clear that when you are writing code you should be writing it behind a namespace if it's any kind of serious project if you guys enjoyed this video please hit the like button you can help support the series by going over to patreon Ocampo slash that sure know you'll get access to videos early and many other awesome rewards thank you so much to all of the lovely patrons that helped bring this series to life it wouldn't be here without you I will see you guys next time goodbye [Music] you
Info
Channel: The Cherno
Views: 117,073
Rating: undefined out of 5
Keywords: thecherno, thechernoproject, cherno, c++, programming, gamedev, game development, learn c++, c++ tutorial, namespace, namespaces, using namespace std, using namespace, tutorial
Id: ts1Eek5w7ZA
Channel Id: undefined
Length: 13min 16sec (796 seconds)
Published: Wed Feb 28 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.