gpt4 book ai didi

c++ - 嵌套类、继承、名称冲突

转载 作者:行者123 更新时间:2023-11-28 02:21:18 25 4
gpt4 key购买 nike

我想创建一个接口(interface)来读取树形式的配置,例如 JSON 或 XML。为此,我考虑了以下接口(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;
};

然后我实现它来读取不同的格式类型(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 );
};

我的问题是:我可以创建一个与父类的嵌套类之一同名的嵌套类吗? ConfigLoaderJSON::Node 和 ConfigLoader::Node 不会因为 ConfigLoader::Node 受到保护而不是私有(private)的而发生冲突吗?

谢谢!

最佳答案

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

不,它们不会发生冲突,因为您将使用范围解析运算符来访问它们中的每一个(ConfigLoader::Node 用于 ConfigLoader 的节点,ConfigLoaderJSON::Node 用于 ConfigLoaderJSON 的节点)。不要将嵌套类设为私有(private)(在这种情况下。因为它在派生类实例中不可访问)

关于c++ - 嵌套类、继承、名称冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32372150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com