1

It is considered that those definitions (members of classes, types etc) that are in the private part are data and types of the class itself. But there are exceptions that I don't understand. For example, why in the example below, do the C++ language rules fail to describe the type with the keyword auto?

class MyClassA 
{

    class MyClassB
    {
    public:
        MyClassB::MyClassB() {};

        MyClassB::~MyClassB() {};

        void get() const {};
    };

    MyClassB m_B;

    public:

        MyClassA() {};
    ~MyClassA() {};

    const MyClassB& get()
    {
        return m_B;
    }
};

 int _tmain(int argc, _TCHAR* argv[]) 
 {
    MyClassA classA;

    const MyClassA::MyClassB& ref1 = classA.get(); //! error C2248: 'MyClassA::MyClassB': cannot access private class declared in class 'MyClassA'

    const auto ref2 = classA.get(); //! OK, but why is it good here?

    ref1.get(); 

    return 1;
}

What does the ะก++ standard say about this?

I am developing code using MSVS2015 (toolset v140)

Oleh Sin
  • 11
  • 1
  • "[...] rules fail to describe the type with the keyword auto?" your text seems to contradict what the code is showing. It is the line without `auto` that fails, no? โ€“ 463035818_is_not_an_ai Sep 20 '17 at 10:24
  • but how this relates to the point of view of Object-oriented programming? We are using type definitions out of sight. Is it normal? โ€“ Oleh Sin Sep 20 '17 at 11:00
  • think of `private` as restricting the acces for you, but the compiler of course knows also privately declared types โ€“ 463035818_is_not_an_ai Sep 20 '17 at 11:10

0 Answers0