Sunday 8 September 2013

How do I refer to a name of class or function that is defined within a namespace?

Tags

How do I refer to a name of class or function that is defined within a namespace?

Ans:
There are two ways in which we can refer to a name of class or function that is 


defined within a namespace: Using scope resolution operator through the using keyword. 

This is shown in following example: 


                             namespace name1
                             {
                             class sample1
                             {
                             // code
                             } ;
                             }
                            namespace name2
                             {
                             class sample2
                             {
                             // code
                             } ;
                             }
                             using namespace name2 ;
                             void main( )
                             {
                             name1::sample1 s1 ;
                             sample2 s2 ;
                             } 



Here, class sample1 is referred using the scope resolution operator. On the other hand we can   directly refer to class sample2 because of the statement using namespace name2 ; the using keyword declares all the names in the namespace to be in the current scope. So we can use the names without any qualifiers. 


EmoticonEmoticon