C++调用公共基类的私有/受保护函数

发布于 2024-10-08 08:33:14 字数 366 浏览 3 评论 0原文

在下面的示例中,是否有一种从 B::bar() 调用 A::foo() 的好方法?

class A {
protected:
  void foo() {}
};

class B : public A {
public:
  void bar(A& a) { // edit: called with &a != this
    a.foo(); // does not work
  }
};

除了声明 BA 的友元之外,我想不出任何其他方法,但是如果有更多的类,这可能会变得非常难看。

有什么想法吗?

Is there a nice way to call A::foo() from B::bar() in the following sample?

class A {
protected:
  void foo() {}
};

class B : public A {
public:
  void bar(A& a) { // edit: called with &a != this
    a.foo(); // does not work
  }
};

I can't think of anything other than declaring B to be a friend of A, but that could get pretty ugly with some more classes.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

诗化ㄋ丶相逢 2024-10-15 08:33:14

是的,您可以使用基类函数。

class A {
protected:
  void foo() {}
  void do_other_foo(A& ref) {
      ref.foo();
  }
};

class B : public A {
public:
  void bar(A& a) { // edit: called with &a != this
    this->do_other_foo(a);
  }
};

Yes, you can use a base-class function.

class A {
protected:
  void foo() {}
  void do_other_foo(A& ref) {
      ref.foo();
  }
};

class B : public A {
public:
  void bar(A& a) { // edit: called with &a != this
    this->do_other_foo(a);
  }
};
热风软妹 2024-10-15 08:33:14

为什么要传递 A 类型的对象?你可以这样做:

class B : public A {
public:
  void bar() {
    foo();
  }
};

或者,像这样

class B : public A {
public:
  void bar() {
    A::foo();
  }
};

Why are you passing object of type A? You could do like this :

class B : public A {
public:
  void bar() {
    foo();
  }
};

or, like this

class B : public A {
public:
  void bar() {
    A::foo();
  }
};
对你的占有欲 2024-10-15 08:33:14

这是一种提供“受保护”访问权限的方法,允许任何派生类或对象进行调用。
它使用受保护的令牌类型,需要解锁特权方法:

struct A
{
protected:
    //Zero sized struct which allows only derived classes to call privileged methods
    struct DerivedOnlyAccessToken{};

public:     //public in the normal sense :
    void foo() {}

public:     //For derived types only :
    void privilegedStuff( DerivedOnlyAccessToken aKey );
};

struct B: A
{
    void doPrivelegedStuff( A& a )
    {
        //Can create a token here
        a.privilegedStuff( DerivedOnlyAccessToken() );
    }
};

int _tmain(int argc, _TCHAR* argv[])
{

    A a;
    a.foo();
    a.privilegedStuff( A::DerivedOnlyAccessToken() ); // compile error.

    B b;
    b.doPrivelegedStuff( a );

    return 0;
}

这不是我的想法。我在某个地方读过它。抱歉,我不记得这是谁的狡猾主意了。

我希望编译器可以省略 aKey 参数。

Here's an approach to giving "protected" like access, allowing calls by any derived classes or object.
It uses a protected token type, required to un-lock privileged methods:

struct A
{
protected:
    //Zero sized struct which allows only derived classes to call privileged methods
    struct DerivedOnlyAccessToken{};

public:     //public in the normal sense :
    void foo() {}

public:     //For derived types only :
    void privilegedStuff( DerivedOnlyAccessToken aKey );
};

struct B: A
{
    void doPrivelegedStuff( A& a )
    {
        //Can create a token here
        a.privilegedStuff( DerivedOnlyAccessToken() );
    }
};

int _tmain(int argc, _TCHAR* argv[])
{

    A a;
    a.foo();
    a.privilegedStuff( A::DerivedOnlyAccessToken() ); // compile error.

    B b;
    b.doPrivelegedStuff( a );

    return 0;
}

This is not my idea. I read it some place. Sorry I dont recall who's cunning idea it was.

I expect the compiler can elide the aKey parameter.

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