我有一个类,其中包含由vector< vector<Node> >
实现的树结构,其中Node
包含一组通过getters/setter公开的属性。使用成员函数打印对象
class Tree
{
vector< vector<Node> > mGrid;
printTree(std::ostream& output = std::cout);
};
class Node
{
double property1 { return mProp1; }
double property2 { return mProp2; }
};
printTree()
目前硬财产使用TSTEP:
void Tree::printTree(ostream& output)
{
...
for (unsigned t = 0; t < mGrid.size(); ++t)
{
toPrint = "";
for (unsigned state = 0; state < mGrid[t].size(); ++state)
{
toPrint += to_string_with_precision(mGrid[t][state].tstep(), 1);
...
有一些华而不实/方便/面向对象推广这一功能的方式,以便它可以打印出任何节点的属性(而不仅仅是吐出硬连线的tstep()属性,或者通过if/then语句实质上做同样的事情)。
我做过的事情像这在C中使用函数指针,但这是C++和C++常见问题解答说不要乱指向成员函数的指针。
你真的想要什么(我认为)是“反射” - 这还不是C++标准的一部分。 : - /这可能会让你感兴趣:https://meetingcpp.com/index.php/br/items/reflections-on-the-reflection-proposals.html –
寻求调试帮助的问题(“为什么这段代码不工作? “)必须包含所需的行为,特定的问题或错误以及在问题本身中重现问题所需的最短代码。没有明确问题陈述的问题对其他读者无益。请参阅:如何创建[mcve]。使用“编辑”链接来改善你的*问题* - 不要通过评论添加更多信息。谢谢! – GhostCat