Basic Structure / Linkage Of Names
Subtitles of the Movie
Ok we're talking about the basic structure of C Plus Plus programming in our C Plus Plus fundamentals video series and since we are in basic structure we need to talk about some things dealing with linkage and linkage of names. Now when we say a name, that's an object, a reference, a function, a type, a template and a namespace, all of those are considered to be names. And linkage of names are where are those names visible, how are they visible to each other and through various translation units. Remember that a C program, and especially a C Plus Plus program can be made up of multiple translation units, and the translation unit is a file basically, a file of source code and what we're looking at is how different translation units see, the objects that are within themselves and within in other translations units, how they are linked together when you have a whole bunch of translation and you've combined them together to be one large program. There's three kinds of linkages, there's external, internal linkage, and none, or no linkage. Alright let's go through and look at those. External linkage means that the items or the item that we are discussing that has external linkage will be visible by all of the translation units in the entire program. A name, remember that was one of many different things, a name that has eternal linkage will be visible only within its own translation unit, it will only be visible to the other names and the other items that are within its source file and then no linkage that's all your local variables and classes that have been defined without external linkage, that's everything else. And there is no linkage to them. Ok, let's go on. Here's an example, here's a linkage of names with external linkage. There you see we have an integer N which is a global non static, its given external linkage, so since it is defined in the, this is the whole file right here, since this integer N is defined outside of any class or other function or namespace or anything like that it is visible everybody and can be used by everybody and is incredibly dangerous because we don't want to have these kind of global non static floating around in the world out there as giving external linkage, that's really old procedural programming and we don't like to do that anymore. You need to encapsulate data like this inside classes like this down in here. Here we have a member function of this class and so it will be visible by using C colon, colon F to get to it but it will have external linkage so anything else in another translation unit could reference this function by using C colon, colon, F the fully qualified name to get to it and they could get to this integer N by using C colon, colon N to get to this static data member. Here we have an external constant called K and we are saying that K is defined some place else. Remember the extern keyword that we used C, well we still use that here, and we are declaring K, want to make sure I use the right word there, we are declaring K to exist and be defined some place else. There can only be one definition and what we talked about that in a couple of videos coming up here but it can be declared all over the place. And here we have a function declaration which is a void function and here we have a new namespace, and this namespace and the class D which would be NS colon, colon D will have external linkage because of the way its declared here and defined. And here is an enum that called direction has up and down at zero and one, talked about enums a little bit ago, and that has external linkage because it is declared outside anything else in this one translation unit, this is the whole source file, translation unit. So that's external linkage, here's internal linkage again we look at a single source file, a single translation unit, static functions have internal linkage so this static F function here is only visible to the other items within this translation unit, here this static integer que, is also declared globally out here but it is only given internally view or linkage because of it being a static. Here's an anonymous namespace, this is a namespace without any tag. Without any identifier, so since it's used this way, we don't have any way of getting a handle on it and referring to it in another translation unit, it doesn't have a name, it puts these items into this namespace but again we can't reference it anyplace else so only the other items that are within this translation unit can get to it. Here's a constant which is not an extern so we are declaring this constant right here and defining it, giving it the value 1000 and since its not declared extern its only good here locally and here's a union, its an anonymous union, again it does not have a name so we have no way of grabbing hold of it and using it anyplace else. But in this one translation unit and here we have a type def name, the type def is only good in this translation unit and that's why we do our type defs in header files and then include them in many different, the top of many different translation units because they don't cross over automatically, you have to include them. And here's no linkage, linkage of names, none, there we have our little integer main, and we have a local class called C, and it is a local class, there is no way to get it by the other translation units, it needs to be declared outside main, outside that function, if we want to be able to see it, in the other translation units, and there's int J is another local object with no external reference, no way for another translation unit to get hold of it. Here we have the object, here's the class we're making a new one called C, no linkage, no reference outside, here's another local enum, this is called parity even and odd, there's the end of main so we see that this enumerated value is created inside of main and we can't peek in there from the other translation units. So it has no linkage as well. Alright so that's external linkage, internal linkage and no linkage of names.
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
- 81,350 Video Tutorials (20,800 free)
- Video Available as Flash or QuickTime
- Over 782 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 