开发者

Splitting task among several functions

I am looking for 开发者_开发问答something which enables me to do something like a function in a function. Here is an example to make it more obvious:

class A{
 private: 
 int n;
 int c;
 public:
 void foo();
}

However foo is a function with is supposed to change c, but needs n for that. foo is somewhat complicated so I want to split it into different subfunctions. Since foo needs n it is not simple doable through a friend function (without passing n (there are tons of variables in my real problem)


Just put all those sub-functions inside the same class and make them private?


class A
{
    int n;
    int c;
    void foo_thing_1();
    void foo_thing_2();
public:
    void foo() { foo_thing_1(); foo_thing_2(); }
};


As it was pointed by other answers already, simple private functions should suffice - unless you also need acess to original function internal variables - which in c++11 is not possible. In upcoming c++0x you might want to look at lambda functions - though i am sure that's not what they were meant for.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜