I'm trying to find how to instantiate an element of a private subclass "before auto", so how to make the second line of the main working fine:
class A{
private:
class B{
public:
void f(){};
};
public:
static B getB(){ return {};};
};
int main(){
auto x1 = A::getB();
A::B x2 = A::getB(); // B is a private member of A , so i can't write A::B
}
It would be quite rare for this to be useful. I would recommend first figuring out why do this rather than how to do it.
There is no way to get A::B
to work outside the scope of A
given private B
. But you can for example provide a public alias in a friend class, and use a similar declaration:
class A{
// ...
friend struct C;
};
struct C {
typedef A::B B;
};
C::B x2 = A::getB();