Search code examples
c++privateinner-classes

How can I access public constructor of private nested class C++


I have a nested private classes Nested and Key and I want that I could using my add_new_object(Key, Nested) function add objects to my my_mmap multimap. But when I try to call that those classes constructors, obviously that it's inaccessible. How can I solve the problem.

class Enclosing
{
private:
    class Nested {
        string m_str;
        double m_dbl;
        bool m_boolean;
    public:
        Nested(string str, double dbl, bool boolean) :m_str(str), m_dbl(dbl), m_boolean(boolean) {};
    };
    class Key {
        int m_number;
        string m_name;
    public:
        Key(int num, string name) :m_number(num), m_name(name) {};
    };

    Enclosing* singleton;

    std::multimap<Key, Nested> my_mmap;
public:
    void add_new_object_to_mmap(Key k, Nested n) {}


    static Enclosing* get_singleton() {
        static Enclosing instance;
        return &instance;
    }
};

Thank you all in advance!


Solution

  • You seem to misunderstand what it means to declare a type in the private section. Or at least you are not aware of the implications of those types appearing as arguments of a public member function.

    Here is your code with definitions added (without a definition for Key and Nested you cannot call their constructor at all):

    #include <map>
    
    
    class Enclosing
    {
    private:
        class Nested {};
        class Key {};
    
        Enclosing* singleton;
    
        std::multimap<Key, Nested> my_mmap;
    public:
        void add_new_object_to_mmap(Key k, Nested n) {}
    
        
        static Enclosing* get_singleton() {
            static Enclosing instance;
            return &instance;
        }
    };
    

    Because the type Key and Nested appear in the public interface of Enclosing they are not actually private. Only their names are private. It is possible to infer the two types from the member function:

    template <typename F> struct get_types;
    
    template <typename K,typename N> struct get_types< void(Enclosing::*)(K,N)> {
        using Key = K;
        using Nested = N;
    };
    

    Now you can call their constructor like you call the constructor of any other type:

    int main()
    {
        using Types = get_types<decltype(&Enclosing::add_new_object_to_mmap)>;
        using Key = Types::Key;
        using Nested = Types::Nested;
        Enclosing::get_singleton()->add_new_object_to_mmap(Key{},Nested{});
    }
    

    Complete Example

    Finally, note that the caller doesn't even have to have names for the two types. This works as well (or with parameters when there are no default constructors):

    Enclosing::get_singleton()->add_new_object_to_mmap({},{});
    

    A type declared in the private section of another class is not private when it appears in the public interface. If you really wanted to hide the two types from the user of Enclosing then you need to do something else. See Teds answer for how to pass only parameters to be passed to their constructor to the public add_new_object_to_mmap. Then the two types can be truly private.