在许多情况下,我发现我的类需要私有函数来分解它们的功能并重用代码.典型的实现方式是:
MyClass.h
#include "AnotherClass.h"
class MyClass {
public:
float foo() const;
private:
float fooPrivate(const AnotherClass& ac) const;
}
Run Code Online (Sandbox Code Playgroud)
MyClass.cpp
#include "MyClass.h"
float MyClass::foo() const {
return fooPrivate(AnotherClass());
}
float MyClass::fooPrivate(const AnotherClass& ac) const {
return ac.foo();
}
Run Code Online (Sandbox Code Playgroud)
这没关系,但在以下情况下,在头文件中声明fooPrivate()可能会有问题:
我们可能不希望在头文件中包含AnotherClass,如果它仅供内部使用,并且在MyClass之外不需要.
如果需要许多私有函数,我们冒着使用不必要的私有函数来污染头文件的风险,这些函数会使代码不太清晰,增加编译时间并且更难以维护.
我知道解决所有这些问题的Pimpl成语,但我的问题是如果我们不想使用Pimpl是否可以为一些函数做这样的事情?
MyClass.h
class MyClass {
public:
float foo() const;
}
Run Code Online (Sandbox Code Playgroud)
MyClass.cpp
#include "MyClass.h"
#include "AnotherClass.h"
static float fooPrivate(const AnotherClass& ac) {
return ac.foo();
}
float MyClass::foo() const {
return fooPrivate(AnotherClass());
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,不需要在MyClass.h中包含AnotherClass.h,除了MyClass.cpp内部以及声明之后,任何人都不能调用fooPrivate().我对吗?
是否有任何警告使用这个或当我的程序变大时我会遇到问题吗?
我有一个使用Qt 5.6制作的iOS应用程序,并通过AppStore上的Xcode 7.2分发.
当我尝试启用BitCode时,我在链接阶段遇到以下错误:
ld: -u and -bitcode_bundle (Xcode setting ENABLE_BITCODE=YES) cannot be used together
Run Code Online (Sandbox Code Playgroud)
我怀疑Qt不是在启用BitCode的情况下构建的,这就是链接器抱怨的原因.我纠正还是我做错了什么?如果它与Qt相关,是否可以在启用了BitCode的情况下为iOS构建Qt版本?
Qt 5.6使用在线安装程序安装.它没有在这台机器上编译.