Using namespace potter custom namespace and usage

Source: Internet
Author: User

Using namespace you will find that there are more than n articles, but basically it is the article that is constantly reposted. I don't think it is very clear, so everyone can understand it, prevent name conflicts...

The usage of custom namespaces and user-defined namespaces has not been fully understood today ....

1. Custom namespace definition:

The first method is to add a custom class to a custom namespace.

Namespace potter {
// Classes can be added here, but they are generally not added in this way.
// Method 1: Write a fixed written class.
// Class string;
}

The second method adds a custom class to a custom namespace.

Using namespace potter;
Class string {// string custom string

}

2. Use a custom namespace:

Upload the demo code I tested so that you can understand it more easily...

1. namespace Class header file: MyNameSpace. h (MyNameSpace. cpp does nothing and does not need to be processed. Create this MyNameSpace class and only create namespaces for simple classes)

[Cpp]
// Classes can be added here, but they are generally not added in this way.
// Method 1: Write a fixed written class.
// Class string;
}
 
Class MyNameSpace // the following code does not matter
{
Public:
MyNameSpace (void );
~ MyNameSpace (void );
};
2. custom string. h

[Cpp]
# Pragma once
# Include "MyNameSpace. h"
Using namespace potter; // The second type is the custom class that is added to the custom namespace.
Class string
{
Public:
String (int );
~ String (void );
Int getId ();
Private:
Int id;
};
3. custom string. cpp

[Cpp]
# Include "StdAfx. h"
# Include "string. h"
 
 
String: string (int id)
{
This-> id = id;
}
 
Int string: getId ()
{
Return id;
}
 
String ::~ String (void)
{
}
4 "TestWin32.cpp (console application portal)
[Cpp]
// TestWin32.cpp: defines the entry point of the console application.
//
# Include "stdafx. h"
# Include <string. h> // It is estimated that two strings are used to generate a conflict.
# Include "Student1.h"
# Include "string. h"
# Include <iostream>
 
Using namespace potter;
 
Void _ tmain (int argc, _ TCHAR * argv [])
{
String s (2 );
Std: cout <s. getId () <std: endl;
Return;
};
5. Running result:

2

 

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.