我在c ++应用程序中看到过一次只使用带有头文件和源文件的命名空间声明,如下所示:
#ifndef _UT_
#define _UT_
#include <string>
#include <windows.h>
namespace UT
{
void setRootPath(char* program_path, char* file_path);
char * ConvertStringToCharP(std::string str);
};
#endif
//and then in UT.cpp
#include "UT.h"
namespace UT
{
char * ConvertStringToCharP(std::string str)
{
char * writable = new char[str.size() + 1];
std::copy(str.begin(), str.end(), writable);
writable[str.size()] = '\0';
return writable;
}
void setRootPath(char* program_path, char* file_path)
{
//...
}
}
Run Code Online (Sandbox Code Playgroud)
比使用静态方法定义经典类更好吗?
还是只是简单的课程?
剂量这种方法对编译器链接器有什么好处?
此命名空间中的方法被称为分配.
在性能方面,拥有static类成员和自由函数之间没有区别namespace.这是一个逻辑问题.你的职能与班级有关吗?
问自己一个很好的问题 - 你是否static为了更好的组织而在课堂上创建成员函数(或者只是为了将它们组合在一起)?如果答案是肯定的,你应该使用a namespace.
| 归档时间: |
|
| 查看次数: |
531 次 |
| 最近记录: |