Username:
Password:
C++ Fundamentals Tutorials

Basic Structure / One Definition Rule

Subtitles of the Movie

Hi welcome back to C Plus Plus Fundamentals we're talking about basic structure of programs right now. And we need to take just a minute and touch on something called the one definition rule. Now it might seem kind of obvious but if your kind of new to C Plus Plus or to this kind of programming you need to understand that it is there so that you don't try to break the rule, because the compiler will not let you. A one definition rule, what this says is that in any translation unit, and that's a source file of source code, a template type function or any other object can only have one definition. Now that's a definition not a declaration, but a definition. An object or non inline function cannot have more then one definition. If an object or function is used it must have exactly one definition. Now you can declare the object or the function or whatever all over the place and that's fine as long as you don't implement it or actually instantiate the thing in multiple places. Ok? Because its going to try to overlay the compiler, the compiler is not going to know which one you want, that's what it comes down to. You can declare an object or function that is never used, so you can have declarations all over as long as you don't define those things in multiple places. Ok? I hope that makes sense. Let's take a little closer look at it. Some things like types, templates and extern inlines can be defined in more then one translation unit. But if you do that the definitions have to be exactly the same so that the compiler understand that you are referring to the same object each time in each one of the declarations and definitions. Non external objects, remember we looked at those just a little bit ago in a previous video when we talked about linkage. Extern linkage and intern linkage and non linkage and non extern objects and functions in the different translation units, the different files are different entities. So their, they don't fall under the one definition rule and they can each be defined and they will each be a different thing and they will each take up their own place in memory. Alright, so there won't be any conflict there, the names and types can be the same but because they don't have external linkage they never run into each other, like people with the same name living in different cities, they'll never know about it, there won't be any problem getting their mail crossed up, alright. So that's the one definition rule, I want to make sure that you understand this and that your aware of it when you start doing some code so that you don't have to chase a new set of bugs.

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