开发者

Internal class and access to external members

I have question with this same title here but now as I'll present in code below this seems to behave i开发者_运维问答n the opposite way to the way explained to me in my first question with the same title. Ok code:

class LINT_rep
{
private:
    char* my_data_; //stores separately every single digit from a number
public:
    class Iterator:public iterator<bidirectional_operator_tag,char>
        {
private: 
char* myData_
public:
        Iterator(const LINT_rep&);
    };
};

#include "StdAfx.h"
#include "LINT_rep.h"


LINT_rep::Iterator::Iterator(const LINT_rep& owner):myData_(nullptr)
{
    myData_ = owner.my_data_; /*
        HERE I'M ACCESSING my_data WHICH IS PRIVATE AND THIS   
        CODE COMPILES ON VS2010 ULTIMATE BUT IT SHOULDN'T  
        BECAUSE my_data IS PRIVATE AND OTHER CLASS SHOULDN'T  
        HAVE ACCESS TO IT'S PRIVATE MEMB. AS EXPLAINED TO ME IN  
        QUESTION TO WHICH I;VE PROVIDED LINK. */
}

Question in the code. Thanks.


Access rights for nested classes to members of the enclosing classes are changing in the upcoming C++0x standard. In the current standard, 11.8 says:

The members of a nested class have no special access to members of an enclosing class

In the draft for C++0x, this changes to

A nested class is a member and as such has the same access rights as any other member

Some compilers are adopting the new access rules; from your question, I guess VS2010 does, and I know from experience that GCC has done for some time.


According to the standard 11.8 a nested class is a member and as a member it has the same rights as the rest of the class members, so it can access private members.

11.8 Nested classes [class.access.nest]
1- A nested class is a member and as such has the same access rights as any other member. The members of an enclosing class have no special access to members of a nested class; the usual access rules (clause 11) shall be obeyed.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜