mercredi 30 décembre 2015

Private nested classes - C++11 compiler misbehavior? Or as designed? [duplicate]

This question already has an answer here:

Consider the following code:

class Main
{
  private:
      class Inner
      {
        public:
          void DoSomething() {}
      };

  public:
    static Inner* CreateInner() { return new Inner(); }
 };

It is impossible to compile the following:

Main::Inner* pInner = Main::CreateInner();
pInner->DoSomething();

Because we'll get a compiler error 'C2248: cannot access private class decalred in class Main'

However, using the following syntax will compile and execute smoothly:

auto pInner = Main::CreateInner();
pInner->DoSomething();

Suddenly, (to my surprise, anyway...), the inner private class is 'visible' to the outside world...

Does anyone aware to this compiler behavior? Is it considered 'valid' to use the auto keyword according to the C++11 standard in this case, or is it some kind of a bug?

It behaves as described on both:

  1. Visual Studio 2010 - platform toolset v100
  2. Visual Studio 2013 - platform toolset v120

Any thoughts?

Thanks!

Aucun commentaire:

Enregistrer un commentaire