嵌套class、中断、名称冲突
Nested class, interitance, name conflict
我想创建一个接口来读取树形式的配置,例如 JSON 或 XML。为此,我考虑了以下界面:
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;
};
然后我实现它以读取不同的格式类型(JSON、XML 等):
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 );
};
我的问题是:我是否可以创建一个嵌套 class,它与父 class 的嵌套 class 之一同名? ConfigLoaderJSON::Node 和 ConfigLoader::Node 不会因为 ConfigLoader::Node 受到保护而不是私有而发生冲突吗?
谢谢!
am i allowed to create a nested class that has the same name as one of
the nested classes of the parent class
是的,你是允许的,但是在派生class中定义的嵌套class会使基础嵌套class隐藏,就像在父子[=]中定义的成员变量一样17=]es.
Aren't ConfigLoaderJSON::Node and ConfigLoader::Node going to be in
conflict due to ConfigLoader::Node being protected and not private
不,它们不会发生冲突,因为您将使用范围解析运算符来访问它们中的每一个(ConfigLoader::Node 用于 ConfigLoader 的节点,ConfigLoaderJSON::Node 用于 ConfigLoaderJSON 的节点)。
不要将嵌套 class 设为私有(在这种情况下。因为它在派生 class 实例中将不可访问)
我想创建一个接口来读取树形式的配置,例如 JSON 或 XML。为此,我考虑了以下界面:
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;
};
然后我实现它以读取不同的格式类型(JSON、XML 等):
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 );
};
我的问题是:我是否可以创建一个嵌套 class,它与父 class 的嵌套 class 之一同名? ConfigLoaderJSON::Node 和 ConfigLoader::Node 不会因为 ConfigLoader::Node 受到保护而不是私有而发生冲突吗?
谢谢!
am i allowed to create a nested class that has the same name as one of the nested classes of the parent class
是的,你是允许的,但是在派生class中定义的嵌套class会使基础嵌套class隐藏,就像在父子[=]中定义的成员变量一样17=]es.
Aren't ConfigLoaderJSON::Node and ConfigLoader::Node going to be in conflict due to ConfigLoader::Node being protected and not private
不,它们不会发生冲突,因为您将使用范围解析运算符来访问它们中的每一个(ConfigLoader::Node 用于 ConfigLoader 的节点,ConfigLoaderJSON::Node 用于 ConfigLoaderJSON 的节点)。 不要将嵌套 class 设为私有(在这种情况下。因为它在派生 class 实例中将不可访问)