| Register | Members | Radio & TV | Games | Quran | Calendar | Search |
| Tutorials/Software Reviews & Tech Support Tutorials to some often repeated & Need help? Got Computer Issues? |
![]() |
| LinkBack | Thread Tools | Rate Thread | Display Modes |
| ||||||||
| Re: Learn C++ Using and Extending Namespaces Most C++ programmers are familiar with the basic concepts of namespaces. And yet, they tend to use namespaces passively, e.g., by yanking code and data from namespaces defined by a third-party. They rarely declare their own classes and functions in namespaces, though. In the following passage I will show that the task of declaring your classes and functions in a namespace isn't as intimidating as it might seem at first. Namespace members As previously said, a namespace is a scope that contains declarations of classes, functions, constants, templates etc. These are called namespace members. For example: Quote:
In the following header file Foo.hpp, I defined a namespace called NS that contains a declaration of class Foo: Quote:
Quote:
Extending a Namespace Namespaces are extensible. You may add more members to the same namespace in other .hpp files, as in the following example: Quote:
Quote:
Quote:
Quote:
Now the question is: how do you use these classes in an application? Referring to a namespace member Inside the main.cpp file, you first have to #include the header files that declare Foo and Bar. Then, then add appropriate using-declaration: Quote:
|
| ||||||
| Re: Learn C++ (for Advance Users) Interaction with Other Language Features Namespaces interact with other features of the language and affect programming techniques. They make some features in C++ superfluous or undesirable, as I will show below. Using the Scope Resolution Operator In some frameworks, it's customary to add operator :: before a function's name to mark it explicitly as a function that is not a class member. For example: Quote:
Using static to Indicate Internal Linkage In standard C, a nonlocal identifier declared static has internal linkage, which means that it's accessible only from the translation unit in which it is declared (linkage types are discussed in the section "Memory Management"). This technique is used to support information hiding as in the following sample: Quote:
Quote:
Standard header files All Standard C++ header files now have to be #included in the following way: Quote:
Quote:
Quote:
Namespaces are an efficient and rather intuitive means of avoiding name conflicts. C++ offers three methods for injecting a namespace constituent into the current scope. The first is a using-directive, which renders all of the members of a namespace visible in the current scope. The second is a using-declaration, which is more selective and enables the injection of a single component from a namespace. Finally, a fully qualified name uniquely identifies a namespace member. While most programmers are familiar with the constructs of bringing an existing namespace member into scope, it's important to know how to define your own namespaces and how to declare its members correctly. You can always extend an existing namespace by adding new members to it. The new members needn't be declared within the pair of { and } of the original namespace declaration. However, it's advisable to group all namespace members' declarations within the same physical file for maintenance and documentation purposes. The argument-dependent lookupcaptures the programmer's intention without forcing him or her to use wearying references to a namespace. |
| ||||
| Re: Learn C++ (for Advance Users) Bhai itna kuch copy paste kar rehay hai app app ko lagta hai koi is ko dekh kay C++ seek jaya ga :) Phelay koi user ata hai jis ko koi help chahiye phir woh topic app achi terah us ki information dey
__________________ ![]() l||l • Kamii • l||l |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 2 (0 members and 2 guests) | |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Web Technology | umair_pak | Science & Technology News and Discussion | 0 | 27-09-2008 03:08 PM |
| Aamir Asks Jiah To Learn How To Act | mkjan | Movies Reviews | News | Events | 0 | 26-08-2008 09:30 AM |
| Notice to Kalshout users | Style Mantra | Announcements | 0 | 14-06-2008 09:21 PM |
| Forumpk's Expert Advice!!!! | Style Mantra | General Discussions | 46 | 03-03-2008 07:16 PM |
| computer users | maha420 | J O K E S !!! | 14 | 26-01-2005 08:53 PM |
| Register | FAQ | Members List | Calendar | Search | Free SMS | Today's Posts | Mark Forums Read |