一种用不同变量做同样的方法

Dir*_*irk 0 c++ templates

我有类似的东西

class Foo {
    Bar a, b, c;

    void doStuffWithA();
    void doStuffWithB();
    void doStuffWithC();
}
Run Code Online (Sandbox Code Playgroud)

而不是为每个我希望像模板这样的方法编写实现.怎么做?

干杯,德克

编辑:

我明确需要知道我做的变量(递归):

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)
        }

    }

}
Run Code Online (Sandbox Code Playgroud)

EDIT2:

也许这确实解释了我的问题是什么:

class Foo {
public:
    int a, b, c;

}

class Bar {
public:
    void doStuffWithAOfFoo(Foo *_foo);
    void doStuffWithBOfFoo(Foo *_foo);
    void doStuffWithCOfFoo(Foo *_foo);

}
Run Code Online (Sandbox Code Playgroud)

我只想保持我的代码简单,而不是必须三次实现doStuffWithX ...

And*_*ite 5

我想你想要参数......

class Foo {
    Bar a, b, c;

    void doStuffWithBar(Bar x);
}
Run Code Online (Sandbox Code Playgroud)

模板用于处理各种数据类型,函数参数用于处理各种变量.