关于C ++内部类的问题 [英] Question about C++ inner class

查看:129
本文介绍了关于C ++内部类的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI,



在C ++内部类别中,

  A {
public:
void f1();
private:
void f2();
class B {
private void f3();
};

}

内部类(B)父类(A)? (就像在Java中)。
B可以调用其父类public / private方法(就像在Java中一样)。



谢谢。



所有改变的是可见性和名称(例如,如果它在外部类不可见的 private:部分,如果它在 public:部分,它是可见的,但(当然)命名它使用 outer_class :: inner_class 。它仍然是一个完全独立的类,只是例如,内部类没有创建任何外部类的实例。



编辑:对不起,我错过了你的问题的一部分在C ++ 0x,内部类有访问到外部类的私有部分 - 在本质上,它好像外部类已经声明内部类作为其朋友,所以私有名称是可见的,但是你仍然需要传递它像一个对象的引用外部类可以调用外部类的任何非静态成员函数。



虽然这不应该是这种情况,但我相信大多数编译器实现这个特定的部分已经。


HI,

In C++ inner class,

class A {
    public: 
         void f1();
    private:
         void f2();
    class B {
       private void f3(); 
    };

 }

Does an inner class (B) has a pointer to its parent class (A)? (like it does in Java). And can B calls its parent class public/private method (like it does in Java).

Thank you.

解决方案

No -- in C++, nesting classes only affects names and visibility, not the semantics of the class itself. As far as generated code goes, the nested class is no different from one that isn't nested.

All that's changed is the visibility and the name (e.g. if it's in a private: section of the outer class, it's not visible to the outside world, and if it's in a public: section, it's visible, but (of course) to name it you use outer_class::inner_class. It's still a completely separate class though -- just for example, you can create an instance of the inner class without creating any instance of the outer class.

Edit: Sorry, I missed part of your question. In C++ 0x, the inner class does have access to the the private parts of the outer class -- in essence, it's as if the outer class has declared the inner class as its friend, so private names are visible, but you still need to pass it something like a reference to an object of the outer class before it can invoke any non-static member functions of the outer class.

Although this isn't supposed to be the case yet, I believe most compilers implement this particular part already.

这篇关于关于C ++内部类的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆