Welcome to reprint, reprint please indicate the source: http://blog.csdn.net/aa4790139/article/details/8096741
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)
Namespace Potter {// class can be added here, but this is generally not the case. // The first method is as follows: a fixed written class is generally written here // class string ;} class mynamespace // the following code can ignore {public: mynamespace (void );~ Mynamespace (void );};
2. custom string. h
# Pragma once # include "mynamespace. H" using namespace Potter; // The second type is the custom class string {public: string (INT) added to the custom namespace );~ String (void); int GETID (); Private: int ID ;};
3. custom string. 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)
// Testwin32.cpp: defines the entry point of the console application. // # Include "stdafx. H "# include <string. h> // It is estimated that two strings are used for 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
After reading this example, I believe you have fully understood it...