开发者

C++, innerclasses and why you can only create members as pointers to them

Why is it that you can only create a pionter to an inner class as a member of the enclosing class. How is it aranged in memory and why does it prevent this, ex:

class temp  
{  
    public:

    class inner
    {
    public:
        inner(int a = 0) : memberInt(a) {}
        const int memberInt;
    };

    temp(int i = 0) : member(i)  
    {  
    }  
    i开发者_运维技巧nner *i; // this works  
    inner i; // this doesn't  
    int member;  
};  

Thanks in advance :-).


The above code actually compiles fine for me in g++, assuming I rename the second one (inner i) to inner i2.

This is the exact code I compiled in g++:

class temp  
{  
    public:

    class inner
    {
    public:
        inner(int a = 0) : memberInt(a) {}
        const int memberInt;
    };

    temp(int i = 0) : member(i)  
    {  
    }  

    inner *i;
    inner i2;
    int member;  
}; 


int main()
{
    return 0;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜