C++如何创建嵌套类来转发您的方法?

发布于 2024-11-25 23:50:21 字数 375 浏览 0 评论 0原文

所以我的想法很简单 - 拥有一个可以拥有自己的公共方法的类和一个仅支持其中一些公共方法(伪代码)的嵌套类:

class API
{
public:
    go();
    stop();
    friend class B
    {
    public:
        void public_request()
        {
            request();
        }
        void public_go()
        {
            go()
        }
    };
private:
    void request(){}
};

是否可以在 C++ 中实现这样的嵌套类以及如何实现?

So my Idea is simple - to have a class that can have its own public methosds and a nested class that would forvard only some of that public methods (pseudocode):

class API
{
public:
    go();
    stop();
    friend class B
    {
    public:
        void public_request()
        {
            request();
        }
        void public_go()
        {
            go()
        }
    };
private:
    void request(){}
};

Is it possible to implement such nested class in C++ and how?

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

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

发布评论

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

评论(3

水染的天色ゝ 2024-12-02 23:50:21

可以用 C++ 创建本地类。局部类是在函数内部定义的类。
但这样的类有几个限制。了解它

不过,您无法像 Java 一样在 C++ 中创建嵌套类。嵌套类是另一个类范围内的类。

嵌套类的名称是其封闭类的本地名称(在封闭类的范围之外不可见)。除非使用显式指针或引用,否则嵌套类中的声明只能使用可见构造,包括封闭类中的类型名称、静态成员和枚举器 对于嵌套类,

需要注意的两个要点是:

  1. 嵌套类的成员函数遵循常规访问规则,并且对其封闭类的成员没有特殊的访问权限。
  2. 封闭类的成员函数对嵌套类的成员没有特殊访问权限。

因此,您需要使用组合或显式地将外部类对象的指针或引用传递给嵌套类

It is possible to create a Local class in C++. Local class is a class defined inside an function.
But such an class has several restrictions. Read about it here in detail.

You cannot create Nested classes in C++ like Java though. Nested class is a class within the scope of another class.

The name of a nested class is local to its enclosing class(It is not visible outside the scope of the enclosing class). Unless you use explicit pointers or references, declarations in a nested class can only use visible constructs, including type names, static members, and enumerators from the enclosing class

Two important points to note with Nested classes are:

  1. Member functions of a nested class follow regular access rules and have no special access privileges to members of their enclosing classes.
  2. Member functions of the enclosing class have no special access to members of a nested class.

So You will need to use composition or explicitly pass a pointer or reference of object of outer class to the nested class

御弟哥哥 2024-12-02 23:50:21

是的,但是当您创建内部类时,您必须提供外部类的实例...这两种类型[大部分]是独立的,因此 B 不与任何特定的 API

更多伪代码

struct API {
   void go();
   struct B {
       B(API& api) : api(api) { }
       void go() { api.go(); }
   };
};

API a;
B b(a);
b.go();

或者,您可以将 API 的实例传递给 B 的函数 [B::go(API&)

struct API {
   void go();
   struct B {
       void go(API& api) { api.go(); }
   };
};

API a1, a2;
B b;
b.go(a1);
b.go(a2);

] ,API 不包含 B 的实例,除非您显式添加实例。

struct API {
  struct B { };
  B b;
};

另请注意,B 不需要通过 API 授予友谊。作为内部类,B 已经可以访问 API 的私有/受保护成员。然而,反之则不然... API 无法访问 B 的私有/受保护成员,除非 B 授予其权限。

Yes, but you have to provide an instance of the outer class when you create the inner one... the two types are [mostly] independant, so B is not associated with any specific instance of API.

More pseudo-code

struct API {
   void go();
   struct B {
       B(API& api) : api(api) { }
       void go() { api.go(); }
   };
};

API a;
B b(a);
b.go();

Alternatively, you can pass an instance of API to B's function [B::go(API&)]

struct API {
   void go();
   struct B {
       void go(API& api) { api.go(); }
   };
};

API a1, a2;
B b;
b.go(a1);
b.go(a2);

Further, API does not contain an instance of B unless you explicitely add an instance.

struct API {
  struct B { };
  B b;
};

Also, note that B does not need to be granted friendship by API. As an inner class, B can already access APIs private/protected members. The converse is not true, however... API can not access Bs private/protected members unless B grants it permission.

执手闯天涯 2024-12-02 23:50:21

不是直接的,至少不是像Java中那样。您的嵌套类必须包含对“外部”类的指针或引用。

Not directly, at least not in the same way as in Java. Your nested class has to contain a pointer or a reference to the "outer" class.

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