C++虚拟+受保护?
在C++中,我有一个基类A,一个子类B。两者都有虚方法Visit。 我想在B中重新定义“Visit”,但是B需要访问每个A(以及所有子类)的“Visit”函数。
我有类似的东西,但它告诉我 B 无法访问 A 的受保护成员!但 B 也是 A :-P
那么,我能做什么呢?
class A
{
protected:
virtual Visit(...);
}
class B : public class A
{
protected:
vector<A*> childs;
Visit(...);
}
B::Visit(...)
{
foreach(A* a in childs)
{
a->Visit(...);
}
}
谢谢
In C++, I have a base class A, a sub class B. Both have the virtual method Visit.
I would like to redefine 'Visit' in B, but B need to access the 'Visit' function of each A (and all subclass to).
I have something like that, but it tell me that B cannot access the protected member of A! But B is a A too :-P
So, what can I do?
class A
{
protected:
virtual Visit(...);
}
class B : public class A
{
protected:
vector<A*> childs;
Visit(...);
}
B::Visit(...)
{
foreach(A* a in childs)
{
a->Visit(...);
}
}
Thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用自己的对象访问受保护的成员,但不能使用替代对象访问受保护的成员,除非它也是您的类的(不仅仅是基类)。
有一个解决方法,就像有一个不继承友谊的解决方法一样。
无论如何,对于这个例子:
You may access a protected member using your own object but you may not access a protected member using an alternative object unless it is also of your class (not simply the base class).
There is a workaround, just as there is a workaround with friendship not being inherited.
In any case with this example:
让 B 成为 A 的朋友:
just make B a friend of A :
虚函数的本质正是你要逃避的。
这里
所有
a
都会调用它对应的Visit函数。没有必要公开从 A 派生,应该使用 protected。
在
A
中,Visit函数不是虚拟的,并创建一个受保护的构造函数,通过继承(以及朋友和 hax)来限制实例化。
如果您告诉更多细节,我们也可以提供更多帮助。
编辑1:如果您正在使用虚拟,请不要忘记虚拟析构函数。
编辑2:试试这个:
A virtual function's essence is exactly which you are escaping from.
Here
all
a
will call it's corresponding Visit function.It is unnecessary to publicly derive from A, you should use protected.
In
A
the Visit function is not virtual, and make a protected constructor,to restrict instantiation through inheratinance (and friends, and hax).
If you tell more details, we can also help more.
EDIT 1: if you are playing with virtuals, do not forget virtual destructors.
EDIT 2: try this:
听听编译器告诉你你的设计已经搞砸了,不要再假装你更了解了。公开访问。更好的是,让它成为非虚拟的:
哦,当你在做的时候,请求委员会添加漂亮的“foreach/in”语法。
[我是认真的,他们正在寻找让 C++ 更易于使用的方法!]
Listen to the compiler telling you your design is screwed and stop pretending you know better. Make Visit public. Better still, make it non-virtual:
Oh and while you're at it petition the Committee to add the nice "foreach/in" syntax.
[I'm serious, they're looking for ways to make C++ easier to use!]
它在“示例”中得到纠正。现在B是A的子类。
It is corrected in the "example". Now B is subclass of A.