Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Differentiating 2 methods with the same name for a pointer

Differentiating 2 methods with the same name for a pointer

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestionannouncement
12 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    What happened when you compiled and ran it? On my system it worked correctly, as the compiler figured out the correct call from the typedef and function signature.

    C Offline
    C Offline
    Code o mat
    wrote on last edited by
    #3

    You mean with the explicit conversion, right? Currently i am unable to test it in runtime because it is part of a bigger project and i still need to do a lot of work before it becomes "runnable". An alternative solution for me could be to create another method (or two methods) with different names and same parameters and then from these call the right Func, but this isn't that elegant (but still less problematic than renaming the functions everywhere)... I mean something like this:

    int Func1(int x) { return Func(x); }
    int Func2(int x, int y) { return Func(x, y); }
    ...
    FUNC_ONE pFuncOne = Func1;
    FUNC_TWO pFuncTwo = Func2;

    > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

    M L 2 Replies Last reply
    0
    • C Code o mat

      You mean with the explicit conversion, right? Currently i am unable to test it in runtime because it is part of a bigger project and i still need to do a lot of work before it becomes "runnable". An alternative solution for me could be to create another method (or two methods) with different names and same parameters and then from these call the right Func, but this isn't that elegant (but still less problematic than renaming the functions everywhere)... I mean something like this:

      int Func1(int x) { return Func(x); }
      int Func2(int x, int y) { return Func(x, y); }
      ...
      FUNC_ONE pFuncOne = Func1;
      FUNC_TWO pFuncTwo = Func2;

      > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

      M Offline
      M Offline
      molesworth
      wrote on last edited by
      #4

      Code-o-mat wrote:

      Currently i am unable to test it in runtime because it is part of a bigger project and i still need to do a lot of work before it becomes "runnable".

      I always have a couple of simple "test apps" on my machines (one C++ and one C#) which I can slot things into to try out if I'm unsure of how to do them, or of usages, or to try simple versions of algorithms, or just to test ideas. Easy to set up, and very handy. :)

      There are three kinds of people in the world - those who can count and those who can't...

      C 1 Reply Last reply
      0
      • C Code o mat

        You mean with the explicit conversion, right? Currently i am unable to test it in runtime because it is part of a bigger project and i still need to do a lot of work before it becomes "runnable". An alternative solution for me could be to create another method (or two methods) with different names and same parameters and then from these call the right Func, but this isn't that elegant (but still less problematic than renaming the functions everywhere)... I mean something like this:

        int Func1(int x) { return Func(x); }
        int Func2(int x, int y) { return Func(x, y); }
        ...
        FUNC_ONE pFuncOne = Func1;
        FUNC_TWO pFuncTwo = Func2;

        > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #5

        Code-o-mat wrote:

        Currently i am unable to test it

        Just paste it into a simple console app and call from main(), takes but a few minutes to test. Your alternative appears to be an equally good solution. Without knowing a lot more about your project it's difficult to offer much more advice. Either way you seem to have the answer at your fingertips. [edit]spelling[/edit]

        C 1 Reply Last reply
        0
        • M molesworth

          Code-o-mat wrote:

          Currently i am unable to test it in runtime because it is part of a bigger project and i still need to do a lot of work before it becomes "runnable".

          I always have a couple of simple "test apps" on my machines (one C++ and one C#) which I can slot things into to try out if I'm unsure of how to do them, or of usages, or to try simple versions of algorithms, or just to test ideas. Easy to set up, and very handy. :)

          There are three kinds of people in the world - those who can count and those who can't...

          C Offline
          C Offline
          Code o mat
          wrote on last edited by
          #6

          I just realized i have made a mistake, in our project the two "same-name" methods have different return types while in the typedefs i used the same for both and this caused the conversion problem, not that it couldn't decide which version of the function to use. Fixing the typedefs seems to have solved it. My bad. :( Thanks for the help anyways, i guess i should pay more attenction to details...

          > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

          1 Reply Last reply
          0
          • L Lost User

            Code-o-mat wrote:

            Currently i am unable to test it

            Just paste it into a simple console app and call from main(), takes but a few minutes to test. Your alternative appears to be an equally good solution. Without knowing a lot more about your project it's difficult to offer much more advice. Either way you seem to have the answer at your fingertips. [edit]spelling[/edit]

            C Offline
            C Offline
            Code o mat
            wrote on last edited by
            #7

            Thank you for the suggestion, i sometimes do that too, should have thought of it this time too, guess i am tired. Aside of this, see my other post[^] for the "solution"...

            > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

            L 1 Reply Last reply
            0
            • C Code o mat

              Thank you for the suggestion, i sometimes do that too, should have thought of it this time too, guess i am tired. Aside of this, see my other post[^] for the "solution"...

              > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #8

              I think your signature(s) say it all. :laugh: I'm sure you'll get it right after a good night's sleep ... and maybe some beer.

              1 Reply Last reply
              0
              • C Code o mat

                Hello folks! This is probably a very simple question i just don't know how to/where to look for the answer...so, i better give you an example:

                //I declare two function pointer types:
                typedef int(* FUNC_ONE)(int);
                typedef int(* FUNC_TWO)(int, int);

                //I create two functions with the same name but different parameters
                int Func(int x)
                {
                return x;
                }

                int Func(int x, int y)
                {
                return x + y;
                }

                //I create 2 pointers and try to assign them values
                FUNC_ONE pFuncOne = Func;
                FUNC_TWO pFuncTwo = Func;

                My question is: how do i tell the compiler to use the one parameter version of Func when assigning the value to pFuncOne and use the two param version of Func when assigning the value to pFuncTwo. As it is written in the example it passes the first pointer assignment but gives error on the second one as it is trying to assign the first, one parameter version of Func to the FUNC_TWO type pointer too. I wonder, if i explicitly specify the types, like this:

                FUNC_ONE pFuncOne = (FUNC_ONE)Func;
                FUNC_TWO pFuncTwo = (FUNC_TWO)Func;

                will it use the correct function address or will it in both cases use the one parameter version just convert it to FUNC_TWO in the second assignment. Thanks for any help in advance. Oh, and happy new year to you all! p.s: i would like to solve this without having to rename the functions.

                > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

                S Offline
                S Offline
                SimonSays
                wrote on last edited by
                #9

                In your first example, the one without the C-style cast, the type system should prevent you from performing an illegal assignment -- in other words the right thing would have to happen or a compiler error be generated. Because the compiler will not allow the following: typedef int(* FUNC_ONE)(int); int Func2Parm(int x, int y) { return x + y; } FUNC_ONE pFuncOne = Func2Parm; Once you introduce the explicit cast, I am not sure what will happen. I would have guessed that the compiler would report an ambiguity error (after all it could cast either pointer that way).

                1 Reply Last reply
                0
                • C Code o mat

                  Hello folks! This is probably a very simple question i just don't know how to/where to look for the answer...so, i better give you an example:

                  //I declare two function pointer types:
                  typedef int(* FUNC_ONE)(int);
                  typedef int(* FUNC_TWO)(int, int);

                  //I create two functions with the same name but different parameters
                  int Func(int x)
                  {
                  return x;
                  }

                  int Func(int x, int y)
                  {
                  return x + y;
                  }

                  //I create 2 pointers and try to assign them values
                  FUNC_ONE pFuncOne = Func;
                  FUNC_TWO pFuncTwo = Func;

                  My question is: how do i tell the compiler to use the one parameter version of Func when assigning the value to pFuncOne and use the two param version of Func when assigning the value to pFuncTwo. As it is written in the example it passes the first pointer assignment but gives error on the second one as it is trying to assign the first, one parameter version of Func to the FUNC_TWO type pointer too. I wonder, if i explicitly specify the types, like this:

                  FUNC_ONE pFuncOne = (FUNC_ONE)Func;
                  FUNC_TWO pFuncTwo = (FUNC_TWO)Func;

                  will it use the correct function address or will it in both cases use the one parameter version just convert it to FUNC_TWO in the second assignment. Thanks for any help in advance. Oh, and happy new year to you all! p.s: i would like to solve this without having to rename the functions.

                  > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

                  M Offline
                  M Offline
                  Moak
                  wrote on last edited by
                  #10

                  Code-o-mat wrote:

                  I declare two function pointer types [...]

                  Have you considered using C++ virtual functions[^]... just a suggestion. :)

                  Webchat in Europe :java: (only 4K)

                  C 1 Reply Last reply
                  0
                  • M Moak

                    Code-o-mat wrote:

                    I declare two function pointer types [...]

                    Have you considered using C++ virtual functions[^]... just a suggestion. :)

                    Webchat in Europe :java: (only 4K)

                    C Offline
                    C Offline
                    Code o mat
                    wrote on last edited by
                    #11

                    Thanks for the suggestion but i don't see how that would apply to this situation.

                    > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

                    M 1 Reply Last reply
                    0
                    • C Code o mat

                      Thanks for the suggestion but i don't see how that would apply to this situation.

                      > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

                      M Offline
                      M Offline
                      Moak
                      wrote on last edited by
                      #12

                      Well, when I saw function pointers I thought that the design pattern you try to implement is something like a callback or interchangeable functionality. In object oriented languages, polymorphism (virtual functions or delegates) provides exactly that and under the hood they are more or less function pointers, with the advantage that the compiler takes care of most things automatically (type safety, calling context). It could be worth to have a look at these possibilities. For using virtual functions define a interface (pure virtual base class) that you can override:

                      class IFunctionInterface
                      {
                      public: virtual int Func(int x) = 0;
                      public: virtual int Func(int x, int y) = 0;
                      };

                      class CFunction1 : public CWhatever, public IFunctionInterface
                      {
                      public:
                      CFunction1 ();
                      virtual ~CFunction1 ();

                      int Func(int x) { return x; }		//implementation of interface
                      int Func(int x, int y) { return x + y; }
                      

                      };

                      class CFunction2 : public CWhatever, public IFunctionInterface
                      {
                      public:
                      CFunction2 ();
                      virtual ~CFunction1 ();

                      int Func(int x) { return x + x; }	//different implementation of interface!
                      int Func(int x, int y) { return x \* y; }
                      

                      };

                      For delegates have a look at Boost library (function/bind/signal) or Fast C++ Delegates[^] here at CodeProject. Hope this helps. :) /M

                      Webchat in Europe :java: (only 4K)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups