关于嵌套类的问题[本地和嵌套]

发布于 2024-11-24 01:50:29 字数 552 浏览 1 评论 0原文

我有一个 A 类。现在它有一个方法:performSomething()。这本质上是将 LINE 作为输入并检查它是什么类型的 LINE 并相应地分支并执行预期的任务;

Class A
{
    performSomething( LINE )
    {
      check LINE TYPE
      switch( LINE ) {
      }
    }
};

现在,我被要求引入一个新的内部类来在其中执行此操作。现在,麻烦来了。我完全不确定他的意思是什么,或者这是否真的有意义!

问题是如果我需要进一步继续,我需要线路(输入参数)。但是,如果我选择将新类设计为本地类(方法内的类),那么我无法访问它[因为本地自动变量不可访问];[我也对此发布了一个问题)。

我也不认为可以通过使用嵌套类(类内的类)来解决这个问题;但是我对此不太确定。

他还坚持认为 LINE 将在嵌套类中可用,因此我不必担心。但令我头疼的是这一行不是实例变量。它只是一个自动变量。

因此,请有人指出我是否可以实现彻底的嵌套类。

谢谢, 莫安尔·帕万。

I have a class A.Now this has a method say performSomething(). This essentially takes a LINE as input and checks what type of LINE it is and branches accordingly and perform the intended task;

Class A
{
    performSomething( LINE )
    {
      check LINE TYPE
      switch( LINE ) {
      }
    }
};

Now, Im being asked to introduce a new inner class to do this operation in it. Now, here comes the trouble. Im not at all sure what he means by that or if that actually makes any sense here!.

Thing is I NEED LINE(input parameter) if I need to proceed further. But if I chose to design the new class as a LOCAL class(class inside method), then I can't access it[as local auto variable are not accessible];[i posted a question on this too).

I neither feel that this can be addressed by using NESTED class(class inside class);But Im, not quite sure about it.

He also insists that the LINE will be available in the nested class and so I need not worry about it. But what's breaking my head is that this line is not an instance variable. It is jus t an AUtomatic variable.

So, please someone point me out if this can be achieved thorough nested classes.

Thanks,
Moanr Pavan.

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

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

发布评论

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

评论(1

电影里的梦 2024-12-01 01:50:29

嗯,到目前为止,代码还有很多错误。

例如,switch 很可能是错误的多态性方法。在 C++ 中,我们通常使用虚拟函数,但有时我们也可以使用函数重载和/或模板(如果实际类型在编译时已知)。

其次,没有说明“内部”类的基本原理,也不清楚这意味着什么。您可以在其他类中包含类,甚至可以在函数中包含类。因此,以下内容在语法上是可以的:

class A
{
    void performSomething( LINE )
    {
        class B {
            static void performSomething(A* that, LINE) { code };
        };
        B::performSomething(this, LINE);
    }
};

您的编译器会接受这一点,但我们必须问:这有什么意义?另请注意,如果我们显式传递 this,则 B::performSomething 只能访问 A 中的成员。

Well, there's quite a bit wrong with the code so far.

For instance, the switch is very likely the wrong approach to polymorphism. In C++ we generally use virtual functions for that, but sometimes we can also use function overloading and/or templates (if the actual types are already known at compile time).

Secondly, there's no rationale stated for an "inner" class, nor is it clear what that means. You can have classes inside other classes, and even classes inside functions. So the following would be syntactically OK:

class A
{
    void performSomething( LINE )
    {
        class B {
            static void performSomething(A* that, LINE) { code };
        };
        B::performSomething(this, LINE);
    }
};

Your compiler will accept this, but we have to ask: what's the point of this? Also, note that B::performSomething can only access the members from A if we explicitly pass this.

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