这就是我想要做的:我想制作一个二维单元格数组。每个单元格可以包含数字或字符串。然后我想将该表传递给我制作的表达式解析器。我需要解析器能够识别表达式中的单元格是否包含数字(很好)或字符串(不好)。
这就是我尝试的方法:我创建了一个抽象基类 CCell
和三个派生类。派生的一个是 CCellEmpty
,一个是 CCellText
,最后一个是 CCellNumber
。
该表是指向基类的指针的二维数组。在表达式解析器中,我需要从 Number 类中获取数字。问题是您无法通过基类指针访问派生类的私有(private)成员。好吧,在基类中我可以为数字创建一个虚拟 getter。在数字类中,它将返回所需的数字,但我还需要为 Text 类和 Empty 类实现它。对于他们两个来说,它都会抛出异常。
注意:我知道可以使用 dynamic_cast
来完成,但我不想那样做。
这是一个好方法吗?我是 C++、面向对象编程和多态性的新手,所以如果有更好的设计方法,我会很高兴听到。我想以正确的方式来做这件事,而不仅仅是让它以某种方式起作用。
这是我现在使用的代码:
#include <string>
class NotTextException{};
class NotNumberException{};
class CCell
{
public:
virtual ~CCell ( void ){}
virtual int GetNumber ( void ) const = 0;
virtual std::string GetText ( void ) const = 0;
};
class CCellEmpty : public CCell
{
public:
virtual int GetNumber ( void ) const {
throw NotNumberException();
}
virtual std::string GetText ( void ) const {
throw NotTextException();
}
};
class CCellText : public CCell
{
public:
CCellText ( const std::string & text )
: m_text(text)
{}
virtual int GetNumber ( void ) const {
throw NotNumberException();
}
virtual std::string GetText ( void ) const {
return m_text;
}
private:
std::string m_text;
};
class CCellNumber : public CCell
{
public:
CCellNumber ( const int num );
virtual int GetNumber ( void ) const {
return m_number;
}
virtual std::string GetText ( void ) const {
throw NotTextException();
}
private:
int m_number;
};
最佳答案
电池和电路板
Is this a good way to do it? I am new to C++, object oriented programming and polymorphism, so if there is a better way to design it I will be glad to hear it.
是的,有更好的方法:您可以使用 boost::variant
来表示单元格对象,并将访问者应用于每个单元格,与 boost::optional
结合使用> 表示不存在单元格对象。
您的单元和电路板类型如下所示:
using cell = boost::variant<int, std::string>;
using board = std::array<boost::optional<cell>, 100>;
只需 2 行代码即可完成。此时,您只需编写一个通用访问者来尝试获取 Element
类型的元素,否则抛出 Except
类型的异常:
template<typename Type, typename Except>
struct element_getter : boost::static_visitor<Type> {
template<typename Other>
Type operator()(Other const&) const { throw Except(); }
Type operator()(Type const& x) const { return x; }
};
用法
此时,您只需根据单元格是否包含元素,使用 boost::apply_visitor
将访问者应用到每个单元格。
使用示例如下:
board b;
b[45] = 42;
b[78] = 108;
auto sum = std::accumulate(begin(b), end(b), 0,
[](int counter, boost::optional<cell> cell) {
return counter +
boost::apply_visitor(
element_getter<int, not_number_error>(),
cell.get_value_or(0)
);
}
);
此代码将计算所有包含数字的单元格,但如果单元格包含字符串,则会抛出异常。
关于c++ - 如何实现包含不同类型单元格的表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30544583/