About C + + reference variables and namespaces

Source: Internet
Author: User

One, C + + reference variables

Beginners C + + is exposed to the reference variable, defining the type as an integral type:

int A;
int &b=a;

Represents the definition of B as a reference variable, the value of a is referenced, and B is the same as the address of a, and if you change the value of B, the value of the a,b will change, but the address of the reference variable B will not change, and the reference variable is similar to the pointer, but it is different and has different characteristics. The following procedure is used as a comparative analysis:

#include <iostream>
using namespace std;
int main ()
{
	int a,b;
	int &c=a;
	int *q;
	q=&a;
	a=1;
	b=2;
	cout<<c<<endl;
	cout<<*q<<endl;
	cout<<&c<<endl;
	cout<<q<<endl;
	q=&b;
	c=b;//b=2 to C   
	cout<<a<<endl;//a into 2 
	cout<<c<<endl;
	cout<<*q<<endl;
	The address of the cout<<&c<<endl;//reference variable is not changed and remains the original address of a 
	cout<<q<<endl;
	return 0;	


Run the results as shown. This shows that the c,*q is a specific value, and &C,Q is the address value, when the Q points to B, Q's address into B's address, the value of a will not change, so that C reference B, A,c value are changed, but the address of C is still the address of a. This is the reference to the variable's related content.

Ii. about namespaces

C + + In consideration of the possibility of duplicate variables, functions, and so on, set the concept of namespaces, for example, in C + + when the use of <iostream.h>, equivalent to call the library function in C, using the global namespace, that is, the early implementation of C + +; When using < Iostream>, the header file does not define a global namespace and must use namespace Std to properly use cout.

This is the use of a using namespace STD, which refers to a standard library-related identifier, and all identifiers in the C + + standard library are defined in a namespace named Std.

At the same time, namespace can also be customized by the user, the following code to illustrate the meaning of namespace:

Displaynamespacedemonstration
#include <iostream>
using namespace std;
Namespace Savitch1 
{
    void greeting ();
}
Namespace Savitch2 
{
    void greeting ();
}
void Big_greeting ();
int main ()
{
    {
        using namespace Savitch2;
        Use SAVICTCH2, STD, global three namespaces
        greeting ();
    {
        using namespace Savitch1;
        Use SAVITCH1, STD, global three namespaces
        greeting ();
    Big_greeting ();
    Use STD and global two namespaces return
     
    0;
}
Namespace Savitch1 
{
    void greeting ()
    {
        cout<< ' hellofromnamespacesavitch1.\n ';
    }
}
namespace Savitch2 
{
    void greeting ()
    {
        cout<< "greetingsfromnamespacesavitch2.\n";
    }
}
void Big_greeting ()
{
    cout<< "abigglobalhello!\n";
}

It can also be changed to use the following:

Displaynamespacedemonstration
#include <iostream>
using namespace std;
Namespace Savitch1 
{
    void greeting ();
}
Namespace Savitch2 
{
    void greeting ();
}
void Big_greeting ();
int main ()
{
    {
        
        ///using SAVICTCH2, STD, global three namespaces
        savitch2::greeting ();
    }
    {
        
        //using SAVITCH1, STD, global three namespaces
        savitch1::greeting ();
    }
    Big_greeting ();
    Use STD and global two namespaces return
     
    0;
}
Namespace Savitch1 
{
    void greeting ()
    {
        cout<< ' hellofromnamespacesavitch1.\n ';
    }
}
namespace Savitch2 
{
    void greeting ()
    {
        cout<< "greetingsfromnamespacesavitch2.\n";
    }
}
void Big_greeting ()
{
    cout<< "abigglobalhello!\n";
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.