Welcome to Forumpk.com Mark forums read | View Forum Leaders
Forumpk.com




Tutorials/Software Reviews & Tech Support Tutorials to some often repeated & Need help? Got Computer Issues?

Reply
LinkBack Thread Tools Rate Thread Display Modes
Learn C++ (for Advance Users)
 
  #61 (permalink)  
Old 13-11-2008, 07:36 PM
Style Mantra's Avatar
R.I.P
 

Join Date: Feb 2006
Posts: 17,047
Country: Users Flag!
Images: 50
Thanks: 29
Thanked 23 Times in 22 Posts
Rep Power: 20
Style Mantra has a spectacular aura aboutStyle Mantra has a spectacular aura about
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:
namespace proj_alpha
{
//the following are members of namespace proj_alpha
class Spy {/*..*/};
void encrypt (char *msg);
const int MAX_AGENTS = 8;
}
In this example, class Spy is implemented in a single source file. Normally, you declare a class in a header file and define its member functions separately, in a separate source file. How do you split a class that is declared as a namespace member into two or more source files?

In the following header file Foo.hpp, I defined a namespace called NS that contains a declaration of class Foo:

Quote:
//Foo.hpp
namespace NS
{
class Foo
{
public:
void f();
void g();
};
}//close NS
Then, in a separate source file called Foo.cpp, I first #include the header file Foo.hpp and define Foo's member functions f() and g():

Quote:
//Foo.cpp
#include "Foo.hpp"
void NS::Foo::f()
{ /*..*/ }

void NS::Foo::g()
{ /*..*/ }
To refer to a namespace member, you need to use the member's fully qualified name. The fully qualified name of class Foo is NS::Foo. So that the compiler knows that NS is a namespace name, the header file Foo.hpp must be #included before any reference to it is made.

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:
//Bar.hpp
namespace NS //extends NS
{
class Bar
{
public:
void a();
void b();
};
}
Alternatively, you can forward declare the class in the namespace and then define it in a separate file:

Quote:
namespace NS //extends NS
{
class Bar; //fwd declaration
}

class NS::Bar //definition of
//previously declared class
{
public:
void a();
void b();
};
Then, in the file Bar.cpp you define the newly-added class's member functions as follows:

Quote:
#include "Bar.hpp"
void NS::Bar::a()
{/*..*/}

void NS::Bar::b()
{/*..*/}
Now both Foo and Bar are members of namespace NS. The compiler and the linker are able to identify these classes as members of the same namespace, although they appear in separate headers. Note that you cannot extend a namespace simply by using a fully qualified name of a new member of an existing namespace. In other words, even if namespace NS has been declared elsewhere and its declaration is visible to the compiler (e.g., because its header file was #included), the following code is ill-formed:

Quote:
#include "Bar.hpp" //namespace NS is now visible
class NS::C //Error, C hasn't been declared
// explicitly as a member of NS
{
public:
void a();
void b();
};
To fix this code, you have to explicitly declare C as a member of namespace NS before defining it.

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:
#include "Bar.hpp"
#include "Foo.hpp"
int main()
{
using NS::Bar; //a using declaration
using NS::Foo; //ditto
Bar b;
Foo f;
f.f();
//...
}
Reply With Quote
  #62 (permalink)  
Old 23-11-2008, 12:03 PM
Style Mantra's Avatar
R.I.P
 

Join Date: Feb 2006
Posts: 17,047
Country: Users Flag!
Images: 50
Thanks: 29
Thanked 23 Times in 22 Posts
Rep Power: 20
Style Mantra has a spectacular aura aboutStyle Mantra has a spectacular aura about
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:
void C::fill (const char * source)
{
::strcpy (this->pbuf, source);
}
This practice is not recommended, though. Many of the standard functions that used to be global are now declared in namespaces. For example, strcpy now belongs to namespace std, as do most of the Standard Library's functions. Preceding a :: to these functions might confuse the compiler's lookup algorithm and mislead human readers. Therefore, either use the function's fully qualified name or use an appropriate using-declaration to refer to such a function.

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:
//File hidden.c
//invisible from other files */
static void decipher(FILE *f);

decipher ("passwords.bin");

//end of file
Although this convention is still supported in C++, it's now considered deprecated. This means that future releases of your compiler might issue a warning message if you declare a nonlocal variable static. To make a function accessible only from within its translation unit, use an unnamed namespace instead. You should do that, for instance, when you migrate from C. For example:

Quote:
//File hidden.cpp

namespace //anonymous
{
void decipher(FILE *f);
}
// now use the function in hidden.cpp.
//No using declarations or directives are needed
decipher ("passwords.bin");
Members of an anonymous namespace can never be seen from any other translation unit, so the net effect is as if they had static linkage. If you declare another function with the same name in an unnamed namespace in another file, the two functions will not clash.

Standard header files
All Standard C++ header files now have to be #included in the following way:

Quote:
#include <iostream> //note: no ".h" extension
The former ".h" extension was omitted from the headers' names. This convention also applies to the standard C header files, with the addition of the letter 'c'affixed to their name. Thus, a C standard header formerly named <xxx.h> is now <cxxx>. For example:

Quote:
#include <cassert> //formerly: <assert.h>
Although the older convention for C headers, such as <xxx.h>, is still supported, it's now considered a deprecated feature and should not be used in new C++ code. The problem is that C <xxx.h> headers inject their declarations into the global namespace. In C++, on the other hand, most standard declarations are declared in namespace std and so are the <cxxx> standard C headers. Recall that you need a using-declaration, a using-directive, or a fully qualified name to access the declarations in the new style standard headers:

Quote:
#include <cstdio>
using std::printf;

void f()
{
printf("Hello World");
}
Summary
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.
Reply With Quote
  #63 (permalink)  
Old 26-11-2008, 11:46 AM
MirzaKamran's Avatar
King Of Heart
 

Join Date: Apr 2003
Location: ~~Dammam~~
Posts: 31,105
Country: Users Flag!
Images: 24
Thanks: 47
Thanked 39 Times in 39 Posts
Rep Power: 37
MirzaKamran will become famous soon enoughMirzaKamran will become famous soon enough
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
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 2 (0 members and 2 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

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

These are the 70 most used thread tags
Tag Cloud
(r) acne scars anti virus beauty tips bhala clean technology dosti ke sms dosti sms dosti sms in hindi dosti sms in urdu eid sms english eid sms forumpk free urdu poetry friendship iz friendship poetry funny eid sms funny poetry funny ramadan sms funny sms funny urdu poetry gama green it greetings & quotes happy eid hindi poetry funny hindi ramadan sms indian tv islamic sms islamic sms collection islamic sms in urdu jat latest / new eid sms latest/new islamic sms love & romantic sms love poetry love sms love sms2 love urdu poetry matka. mobi-number city details nazms new ramadan sms nice sms nokia n series pakistani forum. play online games poems quotes poetry pos quran ramadan sms text messages romantic sms rut sad love potery savar search sharp aquos sms on dosti tariq text messages ultra large ultra slim urdu dosti sms urdu eid sms urdu islamic sms urdu poetry urdu sms xs1
These are the 100 most searched terms
Search Cloud
7 c's of communication amjad islam amjad bakra mandi cap result cplc currency rates desi mast download ringtones earn money earn money online eid sms forumpk forumpk.com free sms ghazal sms graves of prophets inspirational qoutes ketrina load shedding in pakistan mahandi mehndi designs mobile friendship mobile prices mobile ring tones mobile tones new funny sms orkut pakistan richest man pakistan's richest man richest man in pakistan richest man of pakistan richest pakistani ring tones ringtones sahih bukhari sahih bukhari in urdu sms hi sms sms.pk smspk smspk.com standard chartered standard chartered bank umaira ahmed urdu horoscope wasi shah wasi shah poetry worldcall evdo www.kalpoint.com www.orkut.com www.smspk.com ...

All times are GMT +5. The time now is 09:53 PM.
Forumpk.com Online Pakistan Discussion Forums Copyright © 2000-2008 KalPoint.com