类具有相同的接口,但参数的类型不同
classes have same interface but the type of parameter is different
我有一个类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
模板没有提供明显的价值,它只是一个封装数据的获取器和一组,因此为了简单起见,最好将其删除。你似乎需要的是一种可以int
、double
或Vector3d
的类型,以便它们可以放在同一个容器中。为此,在 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
类是NodeInt
和NoteDouble
的基类。 它看起来像这样(与您的相比略有简化(。
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
- 扩展C++生成的代码的模板参数类型名称
- 如何在 c++ 中定义接受不同参数类型的函数向量?
- 在 C++ 中运行时调用模板时,是否可以切换模板的参数类型?
- 将函数参数类型声明为 auto
- 将函数的参数 - 签名从使用 'std::function<T>' 转换为模板参数类型
- 在 C++17 中调用具有不同参数类型的构造函数
- 具有先前参数类型匹配的参数包
- 我想知道为什么"std::unique_ptr<int> foo(新 int)"是合法的,因为"std::<int>unique_ptr"要求输入参数类型应该是"int"?
- 将可变参数类型列表的扩展打包为复杂类型的初始值设定项列表 - 合法吗?
- MSVC 错误:4 个重载中的任何一个都无法转换所有参数类型
- 使用constexpr + auto作为返回和参数类型的奇怪类型推导
- 如何从第一个参数推断第二个参数类型?
- C++模板函数中,指定回调函子/lambda 的参数类型,同时仍允许内联?
- 如何用不同的参数类型和数字回调函数
- C++stoi:这两个重载都无法转换所有参数类型
- 为什么std::{container}::template不能推导其参数类型
- 为模板参数类型中的新对象分配内存
- 为指向成员模板参数的指针推导额外模板参数类型的紧凑方式
- 使用std::conditional中的模板来确定函数参数类型
- C++中的短参数类型