不同 类 的相同功能包含在主 C++ 中
Same function for different classes being include in main C++
正如您可能从标题中注意到的那样,我有多种 classes,特别是 3 个,每个响应不同的树实现。
在Tree1.h
class Tree1 {
public:
struct node{
char label;
node* nHMI_ptr;
node* nHD_ptr;
};
node* root;
Tree1();
~Tree1();
bool Empty();
Tree1::node& Root();
void AddSon(int i, node &n, char l);
/*
*
* (other functions)
*/
}
在Tree2.h
class Tree2 {
public:
struct node{
char label;
node* nHMI_ptr;
node* nHD_ptr;
node* nFather_ptr;
node* nHI_ptr;
};
node* root;
Tree2();
~Tree2();
bool Empty();
Tree2::node& Root();
void AddSon(int i, node &n, char l);
/*
*
* (other functions)
*/
}
在Tree3.h
class Tree3{
public:
Tree3();
~Tree3();
struct node;
struct nodeSon{
struct node* node_ptr;
nodeSon* nextNodeSon_ptr;
};
struct node{
char label;
node* nextNode_ptr;
struct nodeSon* nSon;
};
node* root;
bool Empty();
Tree3::node& Root();
void AddSon(int i, node &n, char l);
/*
*
* (other functions)
*/
}
如您所见,它们具有相同名称或标识符的函数和成员。这是因为我想使用这个函数制作各种更复杂的算法,但棘手的部分是我想让这个算法独立于正在使用的 class 。我想到的第一件事是在我的 main.cpp.
上创建这个算法
在main.cpp
#include <cstdlib>
#include <iostream>
#include "Tree1.h"
//#include "Tree2.h"
//#include "Tree3.h"
void someFunc(node n) {
//do something by calling Tree functions
//E.g. call Empty(), call AddSon(...)
}
int main(int argc, char** argv) {
return 0;
}
我想要实现的是 someFunc(...) 在所有树上工作而不更改任何底层代码,只需启用 #include 之一并禁用其他两个即可。
- 可以吗?
- 我怎样才能做到这一点?
- Is it possible to do it?
是
- How can I accomplish this?
你只要提供一个模板函数即可:
template<class TreeType>
void someFunc(TreeType& treeContext, typename TreeType::node& n) {
//do something by calling Tree functions
//E.g. call Empty(), call AddSon(...)
}
正如您可能从标题中注意到的那样,我有多种 classes,特别是 3 个,每个响应不同的树实现。
在Tree1.h
class Tree1 {
public:
struct node{
char label;
node* nHMI_ptr;
node* nHD_ptr;
};
node* root;
Tree1();
~Tree1();
bool Empty();
Tree1::node& Root();
void AddSon(int i, node &n, char l);
/*
*
* (other functions)
*/
}
在Tree2.h
class Tree2 {
public:
struct node{
char label;
node* nHMI_ptr;
node* nHD_ptr;
node* nFather_ptr;
node* nHI_ptr;
};
node* root;
Tree2();
~Tree2();
bool Empty();
Tree2::node& Root();
void AddSon(int i, node &n, char l);
/*
*
* (other functions)
*/
}
在Tree3.h
class Tree3{
public:
Tree3();
~Tree3();
struct node;
struct nodeSon{
struct node* node_ptr;
nodeSon* nextNodeSon_ptr;
};
struct node{
char label;
node* nextNode_ptr;
struct nodeSon* nSon;
};
node* root;
bool Empty();
Tree3::node& Root();
void AddSon(int i, node &n, char l);
/*
*
* (other functions)
*/
}
如您所见,它们具有相同名称或标识符的函数和成员。这是因为我想使用这个函数制作各种更复杂的算法,但棘手的部分是我想让这个算法独立于正在使用的 class 。我想到的第一件事是在我的 main.cpp.
上创建这个算法在main.cpp
#include <cstdlib>
#include <iostream>
#include "Tree1.h"
//#include "Tree2.h"
//#include "Tree3.h"
void someFunc(node n) {
//do something by calling Tree functions
//E.g. call Empty(), call AddSon(...)
}
int main(int argc, char** argv) {
return 0;
}
我想要实现的是 someFunc(...) 在所有树上工作而不更改任何底层代码,只需启用 #include 之一并禁用其他两个即可。
- 可以吗?
- 我怎样才能做到这一点?
- Is it possible to do it?
是
- How can I accomplish this?
你只要提供一个模板函数即可:
template<class TreeType>
void someFunc(TreeType& treeContext, typename TreeType::node& n) {
//do something by calling Tree functions
//E.g. call Empty(), call AddSon(...)
}