C++ 使用不同变量执行相同操作的一种方法
我有类似的东西C++ 使用不同变量执行相同操作的一种方法,c++,templates,C++,Templates,我有类似的东西 class Foo { Bar a, b, c; void doStuffWithA(); void doStuffWithB(); void doStuffWithC(); } 我不想为每个方法编写一个实现,而是想要一个模板之类的东西。怎么做 干杯, 德克 编辑: 我需要明确知道我使用递归处理哪个变量: class Foo { Bar a, b, c; Foo* parent; static void doRecur
class Foo {
Bar a, b, c;
void doStuffWithA();
void doStuffWithB();
void doStuffWithC();
}
我不想为每个方法编写一个实现,而是想要一个模板之类的东西。怎么做
干杯,
德克
编辑:
我需要明确知道我使用递归处理哪个变量:
class Foo {
Bar a, b, c;
Foo* parent;
static void doRecursiveStuffWithA(Foo *_node) {
if(_node->parent==NULL) {
return;
} else {
doRecursiveStuffWithA(_node->parent)
}
}
static void doRecursiveStuffWithB(Foo *_node) {
if(_node->parent==NULL) {
return;
} else {
doRecursiveStuffWithB(_node->parent)
}
}
static void doRecursiveStuffWithC(Foo *_node) {
if(_node->parent==NULL) {
return;
} else {
doRecursiveStuffWithC(_node->parent)
}
}
}
编辑2:
也许这能更好地解释我的问题:
class Foo {
public:
int a, b, c;
}
class Bar {
public:
void doStuffWithAOfFoo(Foo *_foo);
void doStuffWithBOfFoo(Foo *_foo);
void doStuffWithCOfFoo(Foo *_foo);
}
我只想让我的代码保持简单,不必三次实现doStuffWithX…我想您需要参数
class Foo {
Bar a, b, c;
void doStuffWithBar(Bar x);
}
模板用于处理各种数据类型,函数参数用于处理各种变量 安德鲁·怀特有最简单的答案。如果你想要一个可以做同样的事情但有不同值的函数,它应该有一个参数 在某些情况下,我们合法地需要看起来几乎相同的不同方法,比如getFirstName、setFirstName、getLastName、setLastName。在那里,使用论据反而会破坏目的
那里的建筑非常完美,确实被广泛接受;唯一的问题是打字的繁琐。如果只想避免所有额外的键入,请考虑使用提供代码模板的集成开发环境。Eclipse和VisualStudio以及其他许多工具都肯定会让您选择一个变量并单击按钮来为该变量生成getter和setter。所有的代码都没有任何麻烦。要详细介绍Andrew的解决方案,您可能还需要:
void doStuffWithBar(Bar x, Bar y, Bar z);
如果您实际上有BarX x、BarY y和BarZ z,那么您可能希望在类中重载您的成员函数
class Foo {
BarX x;
BarY y;
BarZ z;
void doStuffWithBar(BarX x);
void doStuffWithBar(BarY y);
void doStuffWithBar(BarZ z);
};
您可能也在寻找这样的解决方案,这是最丑陋的解决方案,我不推荐:
void doStuffWithBar(int n)
{
if(n==0)
doSomethingWithX();
else if(n==1)
doSomethingWithY();
else if(n==2)
doSomethingWithZ();
}
编辑:
代码气味-设计问题?这里的重复让人觉得Bar需要一种新方法:
void Bar::doStuff(Foo &foo);
然后你需要弄清楚什么是公共的、私有的和常量的
编辑:你的编辑稍微改变了一些事情。我现在真的觉得有一些方法可以改进您的设计,例如STL容器和算法。您可以参考:
class Foo {
Bar a, b, c;
void doStuffWithBar(Bar& what)
{
print(what);
bool flag = check(what);
if (!flag)
doStuffWithBar(what);
}
}
您可以使用指向成员的指针:
class Foo {
Bar a, b, c;
typedef Bar (Foo::*PointerToBar);
void doStuffWithBar(PointerToBar selector)
{
print(this->*selector);
bool flag = check(this->*selector);
if (!flag)
doStuffWithBar(selector);
}
}
后一种解决方案更灵活:您可以选择另一个对象和/或另一个成员来继续递归,指向成员的指针是模糊的,很少使用;除非您需要这种灵活性,否则不要使用它们:
class Foo {
Bar a, b, c;
Foo* next;
typedef Bar (Foo::*PointerToBar);
void doStuffWithBar(PointerToBar selector)
{
print(this->*selector);
if (next)
next->doStuffWithBar(selector);
}
}
你说的模板是什么意思?为什么void doStuffBar x不够?因为我想对a,b和c做同样的事情。实际上,在我的示例中,doStuffWithX是一个递归函数…带Bar参数的函数如何解决这个问题?有一件事:ifparent==NULL在静态函数中不起作用,只要Foo*parent不是静态的。请参见编辑,也许是我,我只是被阻塞了…它可能的x需要是某种类型的引用或指针。最后一个是我一开始想的,但正如你所想的,我认为它非常丑陋…所以所有三个成员都是相同类型的对象条?你想让递归函数做完全相同的事情吗,只有一个不同的对象?不同对象的不同变量是完全一样的。然后我认为你应该把doStuffWithBar方法添加到Bar类中。如果需要来自Foo的参数,请在调用该方法时将其作为参数传入。我在上面编辑过。
class Foo {
Bar a, b, c;
Foo* next;
typedef Bar (Foo::*PointerToBar);
void doStuffWithBar(PointerToBar selector)
{
print(this->*selector);
if (next)
next->doStuffWithBar(selector);
}
}