2016-06-16 35 views
0

我有一个静态类,它有几个函数调用的方法。因为我不能将它们作为私有函数添加到头文件中,而且我不希望它们在类之外被看到,所以我在cpp文件中对它们进行了编码。我的问题是,因为我必须编写项目的文档,无论如何,我可以将这些函数添加到标题中吗? (如果不是编码解释,我不喜欢在cpp中编写文档)。头文件中的静态类方法声明C++

例如:

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 
} 
+1

没有“静态类”这样的事情。 –

+0

将代码放在'details'命名空间中。按照惯例,任何内容都可以被视为不应该使用实现细节。 – NathanOliver

+0

@PeteBecker静态类:只有静态函数的类?因为这个问题不能理解这个问题?我可以改变:) –

回答

0

首先你要混淆的术语“方法”和“功能”。一个方法是一个类成员函数(静态或非静态),这是什么myFunction是。函数meMethod1myMethod2而不是方法,它们是非成员函数或“自由”函数。

其次,如果你想要的功能myMethod1myMethod2是calable仅从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(); 
} 

此外,如果你有一个类只有static成员函数(“方法”)和没有成员变量,那么它与namespace没有什么不同。所以在你的情况下你的头文件可能看起来像

#pragma once 

namespace MyStaticClass 
{ 
    /** 
    * i can write the doc here :D 
    */ 
    void myFunction(); 
} 

源文件不必改变从我上面显示。

+0

感谢您的更正:D,这是我可以得到他们在标题中的声明的最开始? –

+0

@LuisRubiera如果你希望这些函数是“私有的”,并且只能从一个单独的源文件中使用,它可以是一个匿名的命名空间(如我所示)或者使非成员函数为static(并且在声明它们之前声明) 。 –

+0

你从哪里得到这个* method *的定义?它不存在于C++标准中。如果你有一个只有静态成员函数(“方法”)并且没有成员变量的类,那么你错了*,那么它与命名空间*没有区别。有很多不同之处。 – SergeyA