是否有任何理由使重写的C++虚函数的权限与基类不同?这样做有危险吗?
例如:
class base {
public:
virtual int foo(double) = 0;
}
class child : public base {
private:
virtual int foo(double);
}
Run Code Online (Sandbox Code Playgroud)
在C++常见问题解答说,这是一个坏主意,但没有说为什么.
我已经在一些代码中看到了这个习惯用法,我相信作者试图让这个类最终,基于一个假设,即不可能覆盖私有成员函数.但是,本文显示了重写私有函数的示例.当然,C++ faq的另一部分建议不要这样做.
我的具体问题:
在派生类和基类中使用不同的虚拟方法权限是否存在任何技术问题?
有没有合理的理由这样做?
Perforce能够实现的.gitignore文件语法的几分之一?
P4IGNORE的Perforce文档显示了基本的忽略和忽略模式:http://www.perforce.com/perforce/r12.1/manuals/cmdref/env.P4IGNORE.html
我可以说,Perforce不支持:
# Ignore file.txt, but not subdir/file.txt
/file.txt
# Ignore directories named foo, but not files named foo
foo/
Run Code Online (Sandbox Code Playgroud)
忽略文件处理中是否还有其他差异?
在C++中,我有一个包含匿名位域结构的类.我想将其初始化为零,而无需手动写出所有字段.
我可以想象将初始化放在三个地方:
这个位域有很多字段,我宁愿不列出所有字段.
例如,请参阅以下代码:
class Big {
public:
Big();
// Bitfield struct
struct bflag_struct {
unsigned int field1 : 1;
unsigned int field2 : 2;
unsigned int field3 : 1;
// ...
unsigned int field20 : 1;
// bflag_struct(); <--- Here?
} bflag;
unsigned int integer_member;
Big *pointer_member;
}
Big::Big()
: bflag(), // <--- Can I zero bflag here?
integer_member(0),
pointer_member(NULL)
{
// Or here?
}
Run Code Online (Sandbox Code Playgroud)
其中一个更好吗?或者还有其他我想念的东西?
编辑:根据下面接受的答案(Ferruccio),我决定采用这个解决方案:
class Big {
// ...
struct bflag_struct {
unsigned …Run Code Online (Sandbox Code Playgroud) c++ ×2
bit-fields ×1
constructor ×1
git ×1
gitignore ×1
ignore ×1
overriding ×1
perforce ×1
struct ×1