您的位置:首页 > 其它

整理: 命名空间

2011-02-22 12:03 190 查看
命名空间的使用在封装工程库时有用, 使工程库的组织清晰, 效果类似于java和c#的那种.

避免了重名和重复功能函数的添加, 使代码重用最大化, 提高自己的工作效率和产能.



调用端:

// 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");
		}
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐