Virus721
Virus721

Reputation: 8315

Nested class, interitance, name conflict

I would like to create an interface for reading a configuration which is in the form of a tree, like JSON or XML. For that I have thought about the the following interface :

class ConfigLoader
{
protected:

    class Node
    {
    public:

        virtual ~Node() { }

        virtual int GetAsInt( const Utf8String & name ) const = 0;

        virtual int GetAsInt( const Utf8String & name, int def ) const = 0;

        virtual bool GetAsBool( const Utf8String & name ) const = 0;

        virtual bool GetAsBool( const Utf8String & name, bool def ) const = 0;

        virtual const Node & GetChild( const Utf8String & name ) const = 0;

        // Etc
    };

public:

    virtual ~ConfigLoader() { }

    virtual void Load( const core::Utf8String & sText ) = 0;
};

Which i then implement for reading different format types (JSON, XML, etc) :

class ConfigLoaderJSON : public ConfigLoader
{
protected:

    class Node : public ConfigLoader::Node
    {
    public:

        virtual ~Node() { }

        virtual int GetAsInt( const Utf8String & name ) const;

        virtual int GetAsInt( const Utf8String & name, int def ) const;

        virtual bool GetAsBool( const Utf8String & name ) const;

        virtual bool GetAsBool( const Utf8String & name, bool def ) const;

        virtual const Node & GetChild( const Utf8String & name ) const;
    };

protected:

    Json::Reader m_oReader;

    JSon::Value m_oRoot;

public:

    virtual void Load( const Utf8String & sText );
};

My question is : am i allowed to create a nested class that has the same name as one of the nested classes of the parent class ? Aren't ConfigLoaderJSON::Node and ConfigLoader::Node going to be in conflict due to ConfigLoader::Node being protected and not private ?

Thank you !

Upvotes: 2

Views: 363

Answers (1)

Ali Kazmi
Ali Kazmi

Reputation: 1458

am i allowed to create a nested class that has the same name as one of the nested classes of the parent class

Yes you are allowed, but the nested class defined in derived class will make the base nested class hidden, just like member variable defined in both parent and child classes.

Aren't ConfigLoaderJSON::Node and ConfigLoader::Node going to be in conflict due to ConfigLoader::Node being protected and not private

No they will not be in conflict because you will use scope resolution operator for accessing each of them (ConfigLoader::Node for ConfigLoader's Node, ConfigLoaderJSON::Node for ConfigLoaderJSON's Node). Dont make nested class private (in this case. because it will not be accessible in derived class instance)

Upvotes: 5

Related Questions