开发者

Question about nesting classes[local and nested]

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 th开发者_StackOverflow中文版at 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜