UnNamed or Anonymous Namespaces in C++ | CPP Programming Video Tutorials

LearningLad823 words

Full Transcript

hi this is Anil and welcome to the video tutorial for the learning light on C++ programming so in this tutorial we're going to learn about the unnamed name spaces or you know the anonymous name spaces in C++ so you know we have learned how to define our name space you know we need to use a keyword Nam space and the Nam space name for example one and then two curly braces and you know between these curly Braes we were writing the variables or the functions or the classes so here un named of the anonymous name space is a name space without a name so we're not going to have any name here you know we're just going to have the namespace keyword and then the curly braces and between that Curly braces we can to write the variables functions or the classes so here you know just to demonstrate that I'm going to create a variable and let's call it as X and then uh I'm going to create a function and let's call it as display and I just going to use C out and we're going to say x a and I'm going to refer X and let's end this line all right now here we have a unnamed name space and when we have a unnamed name space we can refer the members defined inside this name space you know without the help of the using keyword and also without using the scope resolution operator so here I can access this X just by using X and let's initialize this one to let's say 25 that's it now I can call this display function by writing display here that's it now if I build and run this okay we need to write the return type white display right weild and run it you know we get X is 25 so the next thing is we can Define this unnamed name space or the anonymous name space as many many times we want inside this file or you know this Anonymous name space is discontinuous in a file so here so for example you know I can do the function prototyping here you know the display and uh I can Define this name space again here you know the anonymous name space and I can Define the body of this display function so it's going to be void display that's it now if I build and run this and not going to get any error you know we get X is 25 so you know this is about the anonymous name spaces or you know the unnamed name space in C++ the next thing that I want you guys to teach you is you know when you use this Anonymous name space you should be aware of certain things so the first thing is you know this unnamed name space or the anonymous name space is discontiguous inside a file but they does not span files so each file has its own unnamed name space if two files contain unnamed name space those name spaces are unrelated you know here you know we have defined this unnamed Nam space twice and we can see that these name spaces are related because you know in this first declaration we have we have declared the function and in this second declaration you know we have defined our function but you know when we use multiple files and if we use the unnamed name space at that time you know that name spaces in that multiple files are unrelated if a header defines an unnamed name space the name in that name space defines different entities local to each file that includes the header so you know we can say that unlike the other name spaces an unnamed name space is local to a particular file and never spans multiple files all right the next thing is you know when you use this unnamed name space names defined in it are in the same scope as the scope at which the name space is defined so if an unnamed name space is defined in the outermost scope of the file you know then the names in that unnamed name space must differ from the names defined at the global scope now which is nothing but if we Define this a name name space in the outermost scope and as we have done here and at that time the names that uh we going to give to the variables or the functions or the classes in that Global scope should not have the same name as the you know names defined in that unnamed name space so this is it guys this is about some information on the unnamed name spaces in C++ thank you for watching and don't forget to subscribe and I'll see you in the next tutorial

Need a transcript for another video?

Get free YouTube transcripts with timestamps, translation, and download options.

Transcript content is sourced from YouTube's auto-generated captions or AI transcription. All video content belongs to the original creators. Terms of Service · DMCA Contact

UnNamed or Anonymous Namespaces in C++ | CPP Programming ...