类具有相同的接口,但参数的类型不同

classes have same interface but the type of parameter is different

本文关键字:参数 类型 接口      更新时间:2023-10-16

我有一个类TypedNode来存储一些数据:

template <typename Type>
class TypedNode {
public:
TypedNode() {}
void SetNodeData(Type data) { data_ = data; }
Type GetNodeData() { return data_; }
private:
Type data_;
};

然后我可以使用它:

int main() {
TypedNode<int> int_node;
TypedNode<double> double_node;
TypedNode<Vector3d> vector3_node;
int_node.SetNodeData(1);
double_node.SetNodeData(2.3);
vector3_node.SetNodeData(Vector3d(4,5,6));;
}

但是我想定义一个要访问的函数:

void Access(std::list<TypedNode> node_list) {
for (auto node : node_list) {
node.GetNodeData();
// if it is an integer, do one thing
// if it is a double, do another
}
}

该列表需要一个具体的类,但我需要存储任何类型的节点。

一些我更改了 Node 的代码:

class NodeBase {
public:
NodeBase() {}
};
template <typename Type>
class TypedNode : NodeBase {
public:
TypedNode() {}
void SetNodeData(Type data) { data_ = data; }
Type GetNodeData() { return data_; }
private:
Type data_;
};
void Access(std::list<NodeBase> node_list) {
for (auto node : node_list) {
node.GetNodeData();
// if it is an integer, do one thing
// if it is a double, do another
}
}

但是 Access(( 函数只能调用基类的方法。 尽管每个派生类都有一个同名接口 SetNodeData,但它们具有不同的类型。所以它们是不同的。它们不能重写基类中的一个相同接口。

我能做什么?

====

====================================================================这是我的解决方案:

#include <list>
enum NodeType {
kInt,
kDouble,
kVector3,
};
class NodeBase {
public:
NodeBase() {}
virtual int GetDataInt();
virtual double GetDataDouble();
virtual Vector3 GetDataVector3();
NodeType type() const { return type_; }
protected:
void set_type(NodeType type) { type_ = type; }
private:
NodeType type_;
};
class NodeInt : NodeBase {
public:
NodeInt() { set_type(kInt); }
int GetDataInt() override { return data_; }
double GetDataDouble() override { check(false) << "error"; }
Vector3 GetDataVector3() override { check(false) << "error"; }
private:
int data_;
};
class NodeDouble : NodeBase {
public:
NodeDouble() { set_type(kDouble); }
int GetDataInt() override { check(false) << "error"; }
double GetDataDouble() override { return data_; }
Vector3 GetDataVector3() override { check(false) << "error"; }
private:
double data_;
};
void Access(const std::list<NodeBase>& node_list) {
for (auto node : node_list) {
switch (node.type()) {
case kInt: {
int data = node.GetDataInt();
// do something about int
break;
}
case kDouble: {
double data = node.GetDataDouble();
// do something about double
break;
}
case kVector3: {
Vector3 data = node.GetDataVector3();
// do something about Vector3
break;
}
}
}
}

您的TypedNode模板没有提供明显的价值,它只是一个封装数据的获取器和一组,因此为了简单起见,最好将其删除。你似乎需要的是一种可以intdoubleVector3d的类型,以便它们可以放在同一个容器中。为此,在 C++17 中有 std::variant 。编译器不合格的人可以使用基本相同的Boost.Varia,只是也适用于C++98。

#include <variant>
struct Vector3d {int x, y, z;};
using Node = std::variant<int,double,Vector3d>;

当然,当其中有一些重要功能时,您可以拥有std::variant<TypedNode<int>,TypedNode<double>,TypedNode<Vector3d>>。张贴TypedNode没有功能,其他但打字更膨胀。

对于使用相同的界面访问变体,有几种方法。例如,可以使用访问者来完成。这是Node中每种类型的ostream输出的访问者NodeOutput

#include <iostream>
struct NodeOutput {
std::ostream& os_;
NodeOutput(std::ostream& os) : os_{os} {}
void operator()(Vector3d const& v3) 
{
os_ << "Vector3d (" << v3.x <<", "<< v3.y <<", "<< v3.z <<")n";
}
void operator()(double const& d) {os_ << "Double " << d <<"n";}
void operator()(int const& i) {os_ << "Int " << i <<"n";}
};

使用这样的访问者,我们可以为Node编写operator<<

std::ostream& operator<< (std::ostream& os, Node const& v) {
std::visit(NodeOutput{os}, v);
return os;
}

尝试一下。std::list很少使用容器,因此为了简单起见,这里将其替换为std::vector,但它与其他容器的工作方式类似。

#include<vector>
int main()
{
std::vector<Node> nodes;
nodes.emplace_back(42);
nodes.emplace_back(6.66);
nodes.emplace_back(Vector3d{3,2,1});
for (auto& n: nodes) {std::cout << n;}   
}

输出:

Int 42
Double 6.66
Vector3d (3, 2, 1)

据我了解,您基本上想访问NodeBase的覆盖功能。您仍然可以使用模板执行此操作。我不会建议对你的设计进行代码更改,因为我假设你过度简化了它,让我们知道你希望你的代码做什么。

话虽如此,让我们假设NodeBase类是NodeIntNoteDouble的基类。 它看起来像这样(与您的相比略有简化(。

class NodeBase
{
public:
...
virtual void DoSomething()
...
};
class NodeInt : public NodeBase
{
public:
...
virtual void DoSomething() //overridden
{
}
...
};
class NodeDouble : public NodeBase
{
public:
...
void DoSomething()//overriden
{
}
...
};

我们还假设我们的 Access 函数看起来像;

template<typename Type, typename A>
void Access(std::list<TypedNode<Type>, A> node_list)
{
for (auto node : node_list)
{
node.DoSomething();
}
}

请注意,由于TypedNode<Type>,我们的 Access 函数现在可以接受任何类型的包含TypeNode的列表

Access函数的工作只是调用 DoSomething。 它不应该关心它是什么类型。 并且应该根据要调用的内容来推断它,具体取决于我们作为参数传递给 Access 调用的内容。

  • 如果传递给Access的列表是<TypedNode<NodeBase>的类型 希望每个节点调用,NodeBase::DoSomething();
  • 如果传递给Access的列表是您想要的类型<TypedNode<NodeInt>要调用的每个节点NodeInt::DoSomething();
  • 如果传递给Access的列表是<TypedNode<NodeDouble>您的类型 希望每个节点调用,NodeInt::DoSomething();

为此,首先让我们从参数化模板参数继承,而不是从基类继承。

template<typename Type>
class TypedNode : public Type

接下来,我们要为TypedNode类声明和定义DoSomething函数。

template<typename Type>
class TypedNode : public Type
{
...
void DoSomething();
...
};
template<typename Type>
inline void TypedNode<Type>::DoSomething()
{
Type::DoSomething();//this is where the magic happens.
}

请注意,这将允许我们调用DoSomething()Generic Base 类的函数Type::DoSomething();。需要注意的一件事是,当我们使用模板参数初始化对象时,模板参数初始化中使用的类必须具有一个名为DoSomething的类成员,否则在编译过程中我们将收到错误。

例如;

TypedNode<int> intNode;      //wont work because int doesnt have a member DoSomething.
TypeNode<NodeBase> baseNode; //fine.
TypeNode<NodeInt> intNode2;  //fine

最后,为我们产生结果的主要代码。

int main() 
{
std::list<TypedNode<NodeBase>> baseNodeList;
std::list<TypedNode<NodeInt>> intNodeList;
std::list<TypedNode<NodeDouble>> DoubleNodeList;
TypedNode<NodeBase> baseNode;
TypedNode<NodeInt> intNode;
TypedNode<NodeDouble> doubleNode;
baseNodeList.push_back(baseNode);
intNodeList.push_back(intNode);
DoubleNodeList.push_back(doubleNode);
Access(baseNodeList);
Access(intNodeList);
Access(DoubleNodeList);
return 0;
}

这是完整的代码 https://ideone.com/2jEmBO