CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    May 2010
    Posts
    13

    Interesting puzzle + function pointers?

    OK. Say my current algo is:

    1
    2
    3
    4
    5
    6
    7
    function func_a() {
    // 100 lines of code
    }
    if (a==0) {
    // call func_a
    a();
    }


    Now, I want to introduce another variable 'b' which will be contributing for calling of another function func_b as like
    1
    2
    3
    4
    5
    6
    7
    function func_b() {
    // 100 lines of code
    }
    if (b == 0) {
    // call func_b
    b();
    }


    OK. Now the point is these 100 lines of code between two functions i.e. func_a and func_b, is almost same. Almost 90 lines are same as like:

    1
    2
    3
    4
    5
    function a() {
    // Some 80 odd lines
    // 10 lines are different now.
    // Same 10 lines
    }


    I hope it is clear until now. Question is:
    Is there any way to share this code between two functions. I can think of three options as of now:
    1) Forget everything and live with it.
    2) Pass some param.
    3) Do something with function pointers.

    I do not want to implement option no. 2 since it is possible that later I will introduce another param. How can function pointers or something else work here?

    Anyone up for challenge?


    - Hemant

  2. #2
    Join Date
    May 2010
    Posts
    13

    Re: Interesting puzzle + function pointers?

    I really do not know how there numbers appeared into my post. Please ignore them. 1, 2 .. 7 etc.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Interesting puzzle + function pointers?

    Put the common lines of code in another function and call that from a and b.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Interesting puzzle + function pointers?

    What is a? Is it a function or a variable?
    The same question - about b.

    Besides. please use Code tags while posting the <pseudo>code snippets!
    Victor Nijegorodov

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Interesting puzzle + function pointers?

    Is there any way to share this code between two functions. I can think of three options as of now:
    1) Forget everything and live with it.
    2) Pass some param.
    3) Do something with function pointers.
    As GCDEF indicated in post #3, probably the best approach is to put the common lines of code in another function (say func_c) and call func_c from func_a and func_b as required. This assumes that these lines can be split into another function and that the variables etc can be isolated and passed to/from func_c fairly easily.

    There is , however, another way. Put the common code into another file (say func_common) and within func_a and func_b just include this code (ie #include func_common). This isn't the 'normal' use of #include but when code is common between functions which can't be easily separated into a function then this method at least allows the common code to be kept in just one place for maintenance.

    Code:
    func_a()
    {
    ...
    #include "func_common"
    ...}
    
    func_b()
    {
    ...
    #include "func_common"
    ...}
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    May 2010
    Posts
    13

    Re: Interesting puzzle + function pointers?

    GCDEF et All. Thanks for your replies.

    Not accepted because common lines lies at end of func_a and func_b as well. So, basically you are suggesting to call me two functions.


    What I was thinking is this:

    Define two other functions fn_a and fn_b which will have only different lines across them. Create a function pointer. Keep a global function(from where I call func_a and func_b) common as like:
    [code]
    func_a (pass function pointer) {
    // Same lines
    // Call function pointer
    // Same lines.
    }

    Make sense?

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Interesting puzzle + function pointers?

    Not accepted because common lines lies at end of func_a and func_b as well.
    What's wrong with the #include method as per my post #5?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    May 2010
    Posts
    13

    Re: Interesting puzzle + function pointers?

    Quote Originally Posted by 2kaud View Post
    What's wrong with the #include method as per my post #5?

    2kaud,

    I appreciate your answer but to me, at least, this is not a clear solution. Because creating extra files are pain.
    I wanted to something in terms of function pointers/functors, if possible.

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Interesting puzzle + function pointers?

    Quote Originally Posted by hemant.bhargava7 View Post
    GCDEF et All. Thanks for your replies.

    Not accepted because common lines lies at end of func_a and func_b as well. So, basically you are suggesting to call me two functions.


    What I was thinking is this:

    Define two other functions fn_a and fn_b which will have only different lines across them. Create a function pointer. Keep a global function(from where I call func_a and func_b) common as like:
    [code]
    func_a (pass function pointer) {
    // Same lines
    // Call function pointer
    // Same lines.
    }

    Make sense?
    So, call two functions.

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Interesting puzzle + function pointers?

    Quote Originally Posted by hemant.bhargava7 View Post
    OK. Say my current algo is:
    You should take a step back and look at your "algorithm" from a much higher level instead of trying to figure out what lines to move where. You're thinking in the coding level instead of thinking first on the conceptual level.
    OK. Now the point is these 100 lines of code between two functions i.e. func_a and func_b, is almost same. Almost 90 lines are same as like:
    So the question to you is "why are these lines almost the same?" In other words, factor out the characteristics that makes func_a and func_b the same. Then you take what is factored out and ascertain what this factored out piece represents. Then write a function, class, whatever that emulates this piece, and any variance can be supplied with parameters, or member variables, etc.
    3) Do something with function pointers.
    Are you coding in C++? If so, then using function pointers is more than likely not necessary.
    I do not want to implement option no. 2 since it is possible that later I will introduce another param.
    This makes my point.

    Create a design first that, regardless of how many variables are introduced, the design works correctly. More than likely, an array or some container would replace this single variable you have now.

    It is all about design, and not writing C++ code right off the bat and then later on attempting to move code here and there.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    May 2010
    Posts
    13

    Re: Interesting puzzle + function pointers?

    [QUOTE=Paul McKenzie;2129035]You should take a step back and look at your "algorithm" from a much higher level instead of trying to figure out what lines to move where.

    Paul, Thanks for your suggestions.
    But of-course I posted question here after enough stepping back and all. I think my question is not clear to everyone here who is trying to answer it. Let me try to explain it one more time in simpler terms.

    Say I have something like this:
    if (variable a == 1) {
    // Call function_a
    Int param1, param2, param3 = something;
    function_a(param_1, param_2, param_3)
    }

    Now I want to add another piece of code to this when variable b is set as like:
    if ( variable b == 1) {
    // Call function_b
    Int param1; // only need one param in this call.
    function_b(param_1);
    }


    Not definitions of function_a and function_b.

    <retrun_type> function_a(param_1, param_2, param_3) {
    // Some 40 lines of code (*)
    // Some 20 lines of code which are using above 40 lines (**)
    // Lastly, some 40 lines of code which are using above 20 lines. (***)
    }

    <return_type> function_b(param_1) {
    // Some 40 lines of code which are same 40 lines of function function_a (Same as of (*))
    // Some 20 lines of code which are using above 40 lines and are different 20 lines from function_a (Different from (**))
    // Some 40 lines of code which are using above 20 lines and are same as of function_a. (Same as of (***))
    }

    I hope it is very clear until now.

    My concern is to manage this code in the best possible way. I have given
    enough thinking to below options and do not want to do.
    1) Pass parameters and live with it.
    2) Create two functions of same lines across function_a and function_b. (Not accepted)
    3) Waiting..

    My big question here is:
    Can function pointers/functors make my code more clean? And if yes, how?

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Interesting puzzle + function pointers?

    Another option is to set up default parameters with values that aren't valid, such as -1 for ints. Only execute the code that uses those parameters if they contain a valid value. You can also pass in pointers and test for null.

  13. #13
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Interesting puzzle + function pointers?

    Quote Originally Posted by hemant.bhargava7 View Post
    Say I have something like this:
    if (variable a == 1) {
    // Call function_a
    Int param1, param2, param3 = something;
    function_a(param_1, param_2, param_3)
    }

    Now I want to add another piece of code to this when variable b is set as like:
    if ( variable b == 1) {
    // Call function_b
    Int param1; // only need one param in this call.
    function_b(param_1);
    }


    Not definitions of function_a and function_b.

    <retrun_type> function_a(param_1, param_2, param_3) {
    // Some 40 lines of code (*)
    // Some 20 lines of code which are using above 40 lines (**)
    // Lastly, some 40 lines of code which are using above 20 lines. (***)
    }

    <return_type> function_b(param_1) {
    // Some 40 lines of code which are same 40 lines of function function_a (Same as of (*))
    // Some 20 lines of code which are using above 40 lines and are different 20 lines from function_a (Different from (**))
    // Some 40 lines of code which are using above 20 lines and are same as of function_a. (Same as of (***))
    }

    I hope it is very clear until now.

    My concern is to manage this code in the best possible way. I have given
    enough thinking to below options and do not want to do.
    1) Pass parameters and live with it.
    2) Create two functions of same lines across function_a and function_b. (Not accepted)
    3) Waiting..

    My big question here is:
    Can function pointers/functors make my code more clean? And if yes, how?
    Code:
    struct customA
    {
       int m_p1, m_p2, m_p3;
       customA(int p1, int p2, int p3) : m_p1(p1), m_p2(p2), m_p3(p3) {}
       int callMe() { /* code for a */ }
    };
    
    struct customB
    {
       int m_p1;
       customB(int p1) : m_p1(p1) {}
       int callMe() { /* code for b */ }
    };
    
    template <typename functor>
    void generalFunction(functor f)
    {
        // 40 lines same as a and b
        //..
        f.callMe();  // customized function
        //...
        // 40 lines same as a and b
    }
    
    int main()
    {
        if (a == 1) 
            generalFunction(customA(1,2,3));
         else
            generalFunction(customB(6));
    }
    Something like this?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 9th, 2013 at 02:21 PM.

  14. #14
    Join Date
    May 2010
    Posts
    13

    Re: Interesting puzzle + function pointers?

    Yep. Great paul. Something like this.

    Let the answers keep coming from oops word.
    Last edited by hemant.bhargava7; September 9th, 2013 at 01:33 PM. Reason: typo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured