Basic Structure / Scope Of Names
Subtitles of the Movie
Welcome back to C Plus Plus Fundamentals, let's take a little bit of time here to talk about scope of names. Scope determines the lifetime of a name, how long something is visible and useable, so scope also determines the visibility of a name when something is created, when its then released from memory and so on. Scope also says when constructors and destructors of a class are called when a class comes in to scope and is instantiated then the constructor is called to create the items within the class that are to be constructed and when the scope of the class ends, when the class goes out of scope, the destructors are called which perform clean up, and the scope operator for classes and namespaces is two colons, colon, colon and that is used to resolve scope issues if its not obvious if its not an implied local scope if you have to specify the class or the namespace scope you use those two colons. Statement blocks are the two braces, top and bottom brace, open and close brace that you see there, local scope only occurs within the braces so that integer I that you see there is never accessible by anything around it because nothing happens within those blocks, it comes into scope as we come into the brace and then when we leave the closing brace it goes away and its done, it's a local automatic, created on the stack quickly and then just as quickly thrown away. So local scope is great for having little counters and for loops, while loops, some test values, things like that, that are very local but do not hang around in the program for very, local scope. Function scope, the only thing that really has function scope is a label and there we have our little foo function and integer I and character C and then a label. There's a label and it has a colon at the end of it, that's how we know it's a label, and then there's going to be more code under there. That label is only accessible by code within that foo function, within the open and close braces, that label is not visible any place else outside that function. That is called function scope. File scope is for names that are declared outside the braces, they have the scope of the translation unit and sometimes that's called a global and there you see we have an integer X declared outside of foo and it could be used by foo or any other function which is in the translation unit along with that foo function. Because it is not declared within the function, it is outside so it has file scope through out the translation unit. If you wanted to have linkage whole different animal then scope if you want it to have linkage in your other translation units, you call it out by saying extern, int X, which is your declaration that says this integer X is defined someplace else and over here is where we are defining it, this translation unit. And we know that we can only define once, we just talked about that. This also has namespace scope, when we define namespaces or when we have a translation unit without a defined namespace in C Plus Plus that is in fact still the namespace of the translation unit, so sometimes you'll see this referred to as namespace scope even though we have not created an explicit new namespace here. And then this is class scope, names that are declared within a class are local to the class. So here we have a class called C foo, use the uppercase C to note that this is a class not a function, and we have our integer I and character C and more code down below there. Here we are going to make a new class object, we are going to make a new, the type is C foo that's our class and we are going to make a C foo class object, alright so there we go. That's a new thing that takes up memory, it has an integer and a character in it, but the only way we can get to this integer is by using the dot operator, ac foo class object dot I equals something. That's the only way to get to this integer by going through the proper scope definition. So that is class scope because that is local to that class, alright so that's the various kinds of scope that we have in C Plus Plus.
Tutorial Information
| Course: | C++ Fundamentals |
| Author: | Tim Heagarty |
| SKU: | 33797 |
| ISBN: | 1-934743-09-7 |
| Release Date: | 2007-09-14 |
| Duration: | 4 hrs / 55 lessons |
| Captions: | For Online University members only |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 