Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delegating into private parts

Sometimes, C++'s notion of privacy just baffles me :-)

class Foo
{
    struct Bar;
    Bar* p;

public:

    Bar* operator->() const
    {
        return p;
    }
};

struct Foo::Bar
{
    void baz()
    {
        std::cout << "inside baz\n";
    }
};

int main()
{
    Foo::Bar b;   // error: 'struct Foo::Bar' is private within this context

    Foo f;
    f->baz();     // fine
}

Since Foo::Bar is private, I cannot declare b in main. Yet I can call methods from Foo::Bar just fine. Why the hell is this allowed? Was that an accident or by design?


Oh wait, it gets better:

Foo f;
auto x = f.operator->();   // :-)
x->baz();

Even though I am not allowed to name the type Foo::Bar, it works just fine with auto...


Noah wrote:

type names defined within a class definition cannot be used outside their class without qualification.

Just for fun, here is how you can get at the type from outside:

#include <type_traits>

const Foo some_foo();

typedef typename std::remove_pointer<decltype( some_foo().operator->() )>::type Foo_Bar;
like image 236
fredoverflow Avatar asked Jun 01 '10 18:06

fredoverflow


3 Answers

Trying to find anything in the standard that would spell it out in detail but I can't. The only thing I can find is 9.9:

Type names obey exactly the same scope rules as other names. In particular, type names defined within a class definition cannot be used outside their class without qualification.

Essentially, the name of Foo::Bar is private to Foo, not the definition. Thus you can use Bars outside of Foo, you just can't refer to them by type since that name is private.

The name lookup rules for members would also seem to have some effect on this. I don't see anything that specifically references "nested class" and thus they wouldn't be allowed to (if my lack of finding anything in fact is because it's not there).

like image 57
Edward Strange Avatar answered Oct 09 '22 02:10

Edward Strange


I can't provide a full answer, but maybe a starting point. The C++ 1998 specification includes the following code example under paragraph 11.3 [class.access] (p. 175):

class A
{
    class B { };
public:
    typedef B BB;
};

void f()
{
    A::BB x;   // OK, typedef name A::BB is public
    A::B y;    // access error, A::B is private
}

In this example, a private type is "published" through a public typedef. Although it's not the same thing as publishing a type through a member function signature, it's similar.

like image 44
stakx - no longer contributing Avatar answered Oct 09 '22 03:10

stakx - no longer contributing


I think this is by design. You cannot explicitly create instance of Foo::Bar but it could be returned from member functions and then you could pass it to other member functions. This lets you to hide implementation details of your class.

like image 41
Kirill V. Lyadvinsky Avatar answered Oct 09 '22 01:10

Kirill V. Lyadvinsky