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