C++虚拟+受保护?

发布于 2024-10-09 17:12:21 字数 386 浏览 7 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

尹雨沫 2024-10-16 17:12:21

您可以使用自己的对象访问受保护的成员,但不能使用替代对象访问受保护的成员,除非它也是您的类的(不仅仅是基类)。

有一个解决方法,就像有一个不继承友谊的解决方法一样。

无论如何,对于这个例子:

class A
{
protected:
  virtual void Visit(...);

  void visitOther( A& other, ... )
  {
     other.Visit(...);
  }
};



class B : public A
   {
       Visit(...);
       vector<A*> childs;
   };
       
   B::Visit(...)
    {
     for( auto a : childs )
     {
         visitOther( *a, ... );
     }
   }

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:

class A
{
protected:
  virtual void Visit(...);

  void visitOther( A& other, ... )
  {
     other.Visit(...);
  }
};



class B : public A
   {
       Visit(...);
       vector<A*> childs;
   };
       
   B::Visit(...)
    {
     for( auto a : childs )
     {
         visitOther( *a, ... );
     }
   }
故事未完 2024-10-16 17:12:21

让 B 成为 A 的朋友:

class A  
{  
protected:  
    virtual void Visit();  
    friend class B;  
}; 

class B : public A  
{  
protected:  
    virtual void Visit();  
};

just make B a friend of A :

class A  
{  
protected:  
    virtual void Visit();  
    friend class B;  
}; 

class B : public A  
{  
protected:  
    virtual void Visit();  
};
甜心小果奶 2024-10-16 17:12:21

虚函数的本质正是你要逃避的。
这里

foreach (A * in the Childs)
{
  a-> Visit (...);
}

所有a都会调用它对应的Visit函数。

没有必要公开从 A 派生,应该使用 protected。

A中,Visit函数不是虚拟的,并创建一个受保护的构造函数,
通过继承(以及朋友和 hax)来限制实例化。

如果您告诉更多细节,我们也可以提供更多帮助。

编辑1:如果您正在使用虚拟,请不要忘记虚拟析构函数。

编辑2:试试这个:

foreach (A * in the Childs)
{
  a->A::Visit(...);
}

A virtual function's essence is exactly which you are escaping from.
Here

foreach (A * in the Childs)
{
  a-> Visit (...);
}

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 (A * in the Childs)
{
  a->A::Visit(...);
}
伪心 2024-10-16 17:12:21

听听编译器告诉你你的设计已经搞砸了,不要再假装你更了解了。公开访问。更好的是,让它成为非虚拟的:

struct A { 
  void Visit() { impl_visit(); }
private:
  virtual void impl_visit();
};

struct B : A {
private:
  Vector<A*> childs;
  void impl_visit() {
    ... 
    foreach child in childs child->Visit();
    ...
  }
};

哦,当你在做的时候,请求委员会添加漂亮的“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:

struct A { 
  void Visit() { impl_visit(); }
private:
  virtual void impl_visit();
};

struct B : A {
private:
  Vector<A*> childs;
  void impl_visit() {
    ... 
    foreach child in childs child->Visit();
    ...
  }
};

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!]

稳稳的幸福 2024-10-16 17:12:21

它在“示例”中得到纠正。现在B是A的子类。

It is corrected in the "example". Now B is subclass of A.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文