search

Home  >  Q&A  >  body text

关于c++嵌套类的使用

class Array
{
    // ...

public:
    class Con_Array
    {
        void printAll();
    };

private:
    Con_Array *con;
};

在main中调用 :

    Array a;
    ( a[0] ).printAll();

( a[0] )代表Array中的一个Con_Array实例,在main使用( a[0] )调用printAll()时为什么会通过,按理main中对Con_Array是不可见的。

我没说清楚,就是说a[0]是外部类所生成的一个内部类Con_Array对象,即con,问题是在main中我使用才·con来调用它的Con_Array的成员函数printAll()。在main中你看不到Con_Array的定义的,为什么能编译通过呢?

PHPzPHPz2819 days ago744

reply all(2)I'll reply

  • PHPz

    PHPz2017-04-17 11:09:30

    I guess you provided the operator[] function and returned ConArray * The class permissions of Array::ConArray are public, so the class definition can be seen externally

    但是Con_Array本身并没有写
    {
    public:
    void printAll();
    }
    

    Then I don’t know how you can compile it. . .

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 11:09:30

    If a[0] returns the Con_Array type, you can call the printAll method
    Nested classes in C are no different from ordinary classes except that you need to include the Parent::Class header when using them

    reply
    0
  • Cancelreply