header c++ 中的静态 class 方法声明
Static class methods declaration in header c++
我有一个静态 class,它有一些从其函数中调用的方法。因为我不能将它们作为私有函数添加到 header 文件中,并且我不希望它们在 class 之外可见,所以我将它们编码在 cpp 文件中。我的问题是,因为我必须编写项目的文档,所以我是否可以将这些功能添加到 header? (如果不是编码解释,我不喜欢在cpp中写文档)。
示例:
MyStaticClass.h
class MyStaticCLass{
public:
/**
* I can write the doc here :D
*/
static void myFunction();
}
MyStaticClass.cpp
void MyStaticClass::myFunction(){
myMethod1();
myMethod2();
}
/**
*I want to write the doc of this function but in the header
*/
void myMethod1(){
//do something 1
}
/**
*I want to write the doc of this function but in the header
*/
void myMethod2(){
// do something 2
}
首先,您混淆了术语 "method" 和 "function"。方法是 class 成员函数(静态或非静态),这就是 myFunction
。函数 meMethod1
和 myMethod2
是 不是 方法,它们是非成员函数,或 "free" 函数。
其次,如果您希望函数 myMethod1
和 myMethod2
只能从 MyStaticClass.cpp
源文件中调用,那么我建议您将它们放入自己的匿名 namespace
:
#include "MyStaticClass.h"
// An anonymous namespace
namespace
{
/**
*i want to write the doc of this function but in the header
*/
void myMethod1(){
//do something 1
}
/**
*i want to write the doc of this function but in the header
*/
void myMethod2(){
// do something 2
}
}
void MyStaticClass::myFunction(){
myMethod1();
myMethod2();
}
此外,如果您有一个 class 只有 static
成员函数 ("methods") 而没有成员变量,那么它与 namespace
没有什么不同。所以在你的情况下你的头文件可能看起来像
#pragma once
namespace MyStaticClass
{
/**
* i can write the doc here :D
*/
void myFunction();
}
源文件不必更改我上面显示的内容。
我有一个静态 class,它有一些从其函数中调用的方法。因为我不能将它们作为私有函数添加到 header 文件中,并且我不希望它们在 class 之外可见,所以我将它们编码在 cpp 文件中。我的问题是,因为我必须编写项目的文档,所以我是否可以将这些功能添加到 header? (如果不是编码解释,我不喜欢在cpp中写文档)。
示例:
MyStaticClass.h
class MyStaticCLass{
public:
/**
* I can write the doc here :D
*/
static void myFunction();
}
MyStaticClass.cpp
void MyStaticClass::myFunction(){
myMethod1();
myMethod2();
}
/**
*I want to write the doc of this function but in the header
*/
void myMethod1(){
//do something 1
}
/**
*I want to write the doc of this function but in the header
*/
void myMethod2(){
// do something 2
}
首先,您混淆了术语 "method" 和 "function"。方法是 class 成员函数(静态或非静态),这就是 myFunction
。函数 meMethod1
和 myMethod2
是 不是 方法,它们是非成员函数,或 "free" 函数。
其次,如果您希望函数 myMethod1
和 myMethod2
只能从 MyStaticClass.cpp
源文件中调用,那么我建议您将它们放入自己的匿名 namespace
:
#include "MyStaticClass.h"
// An anonymous namespace
namespace
{
/**
*i want to write the doc of this function but in the header
*/
void myMethod1(){
//do something 1
}
/**
*i want to write the doc of this function but in the header
*/
void myMethod2(){
// do something 2
}
}
void MyStaticClass::myFunction(){
myMethod1();
myMethod2();
}
此外,如果您有一个 class 只有 static
成员函数 ("methods") 而没有成员变量,那么它与 namespace
没有什么不同。所以在你的情况下你的头文件可能看起来像
#pragma once
namespace MyStaticClass
{
/**
* i can write the doc here :D
*/
void myFunction();
}
源文件不必更改我上面显示的内容。