Gho*_*314 25 c++ standards conventions
我一直认为头文件是一种描述类的"公共接口",在这种情况下,最好将私有字段和函数保存在cpp文件中.
我知道私有字段需要在头文件中,以便其他类可以告诉一个类的实例将消耗多少内存,但是当我要写一个私有帮助函数时,它就出现了,这个函数可以被创建静态,在这种情况下根本不需要它是"类的一部分",它可以很容易地成为类定义的.cpp文件中的常规函数.
然后我想到,通过接受指向类字段的指针/引用而不是期望在类中定义,所有私有函数都可能被重写为静态函数.
这将消除在头文件中声明任何私有函数的需要.
我喜欢遵循约定所以现在我想问一下,它是否被认为是C++中的既定约定,非静态私有函数应该在头文件中?静态函数或静态常量怎么样?
编辑:我将提供一些代码来解释我所得到的:
.h文件:
#ifndef SOME_CLASS_H
#define SOME_CLASS_H
class SomeClass
{
private:
int x;
public:
void combineWithX(int y);
};
#endif
Run Code Online (Sandbox Code Playgroud)
.cpp文件
#include "SomeClass.h"
void someHelper(int* x)
{
*x = (*x) + 1;
}
void SomeClass::combineWithX(int y)
{
someHelper(&x);
x += y;
}
Run Code Online (Sandbox Code Playgroud)
请注意,someHelper(int* x)
在cpp文件中引用私有成员x,但不是直接引用,因此不需要出现在标题中.我想知道这种事情是否被认为是"坏风格"
bal*_*lki 19
只需要PImpl习语
如果您只想将私有成员'函数'移出公共头,那么使用内部类就足够了.这没有像PImpl惯用语那样的重定向惩罚.
公共.h文件
#ifndef SOME_CLASS_H
#define SOME_CLASS_H
class SomeClass
{
private:
struct Private;
int x;
public:
void combineWithX(int y);
};
#endif
Run Code Online (Sandbox Code Playgroud)
在.cpp文件中
#include "SomeClass.h"
/** Declare all private member functions of SomeClass here
This class can access not only private members of SomeClass
but also friends of SomeClass. */
struct SomeClass::Private
{
static void someHelper(SomeClass& self)
{
self.x = self.x + 1;
}
};
void SomeClass::combineWithX(int y)
{
Private::someHelper(*this);
x += y;
}
Run Code Online (Sandbox Code Playgroud)
SomeClass::Private
可以拥有任何数量的辅助函数,它们可以完全访问所有私有/朋友SomeClass
,而不必在头文件中声明它们中的任何一个.