整理: 命名空间
2011-02-22 12:03
190 查看
命名空间的使用在封装工程库时有用, 使工程库的组织清晰, 效果类似于java和c#的那种.
避免了重名和重复功能函数的添加, 使代码重用最大化, 提高自己的工作效率和产能.
调用端:
DLL端:
避免了重名和重复功能函数的添加, 使代码重用最大化, 提高自己的工作效率和产能.
调用端:
// testLsOpensslAppDll.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "testLsOpensslAppDll.h" #include "LsOpensslAppDll_Export.h" #include "TestClass.h" #pragma comment(lib, "LsOpensslAppDll.lib") #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // The one and only application object CWinApp theApp; using namespace std; int fnTestLsOpensslAppDll_Interface(); int fnTestLsOpensslAppDll1_ExportClass(); int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) { int nRetCode = 0; // initialize MFC and print and error on failure if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { // TODO: change error code to suit your needs cerr << _T("Fatal Error: MFC initialization failed") << endl; nRetCode = 1; } else { // TODO: code your application's behavior here. CString strHello; strHello.LoadString(IDS_HELLO); cout << (LPCTSTR)strHello << endl; fnTestLsOpensslAppDll_Interface(); fnTestLsOpensslAppDll1_ExportClass(); } getchar(); return nRetCode; } int fnTestLsOpensslAppDll_Interface() { /** * 命名空间区分大小写 */ ls::opensslapp::test();/**< 使用名字空间的全名 */ /** run results * Ls::opensslapp::test */ return S_OK; } int fnTestLsOpensslAppDll1_ExportClass() { namespace alg = ls::opensslapp;/**< 使用名字空间的别名 */ alg::CTestClass myClass;/**< 使用名字空间中的类 */ myClass.WhoAreU(); /** run results * I'm CTestClass */ return S_OK; }
DLL端:
; LsOpensslAppDll.def : Declares the module parameters for the DLL. LIBRARY "LsOpensslAppDll" DESCRIPTION 'LsOpensslAppDll Windows Dynamic Link Library' EXPORTS ; Explicit exports can go here test
//LsOpensslAppDll_Export.h //LsOPENSSLAPPDLL_EXPORT_H #ifndef LsOPENSSLAPPDLL_EXPORT_H #define LsOPENSSLAPPDLL_EXPORT_H #ifdef LSOPENSSLAPPDLL_EXPORT #define LSOPENSSLAPPDLL_API __declspec(dllexport) #else #define LSOPENSSLAPPDLL_API __declspec(dllimport) #endif namespace ls { namespace opensslapp { LSOPENSSLAPPDLL_API int __stdcall test(); } } #endif
// TestClass.h: interface for the CTestClass class. // _TESTCLASS_H_ ////////////////////////////////////////////////////////////////////// #if !defined(_TESTCLASS_H_) #define _TESTCLASS_H_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include "LsOpensslAppDll_Export.h" namespace ls { namespace opensslapp { class LSOPENSSLAPPDLL_API CTestClass { public: CTestClass(); virtual ~CTestClass(); public: void WhoAreU(); }; } } #endif // !defined(_TESTCLASS_H_)
// LsOpensslAppDll.cpp : Defines the initialization routines for the DLL. // #include "stdafx.h" #include "LsOpensslAppDll.h" #include "LsOpensslAppDll_Export.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif // // Note! // // If this DLL is dynamically linked against the MFC // DLLs, any functions exported from this DLL which // call into MFC must have the AFX_MANAGE_STATE macro // added at the very beginning of the function. // // For example: // // extern "C" BOOL PASCAL EXPORT ExportedFunction() // { // AFX_MANAGE_STATE(AfxGetStaticModuleState()); // // normal function body here // } // // It is very important that this macro appear in each // function, prior to any calls into MFC. This means that // it must appear as the first statement within the // function, even before any object variable declarations // as their constructors may generate calls into the MFC // DLL. // // Please see MFC Technical Notes 33 and 58 for additional // details. // ///////////////////////////////////////////////////////////////////////////// // CLsOpensslAppDllApp BEGIN_MESSAGE_MAP(CLsOpensslAppDllApp, CWinApp) //{{AFX_MSG_MAP(CLsOpensslAppDllApp) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CLsOpensslAppDllApp construction CLsOpensslAppDllApp::CLsOpensslAppDllApp() { // TODO: add construction code here, // Place all significant initialization in InitInstance } ///////////////////////////////////////////////////////////////////////////// // The one and only CLsOpensslAppDllApp object CLsOpensslAppDllApp theApp; /** * 使用名字空间的嵌套 * 使工程库的用途清晰 * * 使不同用途的工程库升级维护的目的性, 条理性更强 * 避免了添加不必要的工具函数或类, 使代码重用最大化 */ namespace ls { namespace opensslapp { LSOPENSSLAPPDLL_API int __stdcall test() { AFX_MANAGE_STATE(AfxGetStaticModuleState()); printf("Ls::opensslapp::test/n"); return S_OK; } } }
// TestClass.cpp: implementation of the CTestClass class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "TestClass.h" ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// namespace ls { namespace opensslapp { CTestClass::CTestClass() { } CTestClass::~CTestClass() { } void CTestClass::WhoAreU() { printf("I'm CTestClass/n"); } } }
相关文章推荐
- 今天你整理命名空间了吗? namespace using 优化了吗?【更适合菜鸟阅读】
- JAXB命名空间及前缀_动力节点Java学院整理
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(四):使用 命名空间 简化配置
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(四):使用 命名空间 简化配置
- TypeScript 命名空间整理
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(四):使用 命名空间 简化配置
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(四):使用 命名空间 简化配置
- 今天你整理命名空间了吗? namespace using 优化了吗?【更适合菜鸟阅读】
- c#得到本地ip地址、Mac地址,using哪个命名空间?(整理)
- 今天你整理命名空间了吗? namespace using 优化了吗?【更适合菜鸟阅读】
- 今天你整理命名空间了吗? namespace using 优化了吗?【更适合菜鸟阅读】
- 今天你整理命名空间了吗? namespace using 优化了吗?【更适合菜鸟阅读】
- JAXB命名空间_动力节点Java学院整理
- C#系统命名空间[精心整理的…
- C++ 命名空间整理
- C#系统命名空间[精心整理的…
- js命名空间写法示例
- using 命名空间指令和cookie保存实体类的一些问题
- C++学习笔记十八-命名空间
- 添加了引用,也引用了命名空间,但还是提示“未找到类型或命名空间(是否缺少程序集指令)”