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. C++ Function Alias

C++ Function Alias

Scheduled Pinned Locked Moved C / C++ / MFC
c++algorithmslearning
17 Posts 8 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.
  • A Albert Holguin

    The obvious way would be:

    class cl
    {
    public: //or private, but with your example above,
    // the external interface for a class should be public
    CString short_l(int i){ return FunctionWithADisturbingLongName(i);}
    }

    Now, this has the added clarity of still allowing the compiler to point you in the correct direction for debugging errors. Using a macro would probably switch names on you all the time (if there's an error it'll report the substituted string).

    C Offline
    C Offline
    Chris Meech
    wrote on last edited by
    #6

    This is probably the safest and most sensible way of accomplishing what the OP has requested. :)

    Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

    1 Reply Last reply
    0
    • C Chris Meech

      I could sense the tears falling as I typed in that answer. :)

      Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

      A Offline
      A Offline
      Albert Holguin
      wrote on last edited by
      #7

      :laugh: ...I still five'd the answer since it would probably work.

      L 1 Reply Last reply
      0
      • J Joschwenk666

        Hi, I´m searching for the best way, to define some kind of as function alias in C++ I have the following class:

        class c1
        {
        private:
        CString FunctionWithADisturbingLongName(int i);
        }

        And i want the function to have the long function name, because the function of the function has to be explained in it :). But I want to have a shortcut to the function. I want to do this

        c1 test;
        test.short_1(1);

        instead of

        c1 test;
        test.FunctionWithADisturbingLongName(1);

        Of course I could declare and define short_1 as a member of c1, and then call FunctionWithADisturbingLongName in it, but there has to be a better solution. Thank you for helping!

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

        Function pointer or wrap the big named func in a small one. Oh yeah, function pointers suck in C++. Awfull damn syntax.

        ============================== Nothing to say.

        1 Reply Last reply
        0
        • J Joschwenk666

          Hi, I´m searching for the best way, to define some kind of as function alias in C++ I have the following class:

          class c1
          {
          private:
          CString FunctionWithADisturbingLongName(int i);
          }

          And i want the function to have the long function name, because the function of the function has to be explained in it :). But I want to have a shortcut to the function. I want to do this

          c1 test;
          test.short_1(1);

          instead of

          c1 test;
          test.FunctionWithADisturbingLongName(1);

          Of course I could declare and define short_1 as a member of c1, and then call FunctionWithADisturbingLongName in it, but there has to be a better solution. Thank you for helping!

          M Offline
          M Offline
          Maximilien
          wrote on last edited by
          #9

          Me think that it is a bad idea. Your code will be peppered with function calls to short and/or abbreviated method names that will not reflect what it actually do; and the user (other developers) will need to look at the documentation to know what it should be doing.

          Watched code never compiles.

          C J 2 Replies Last reply
          0
          • A Albert Holguin

            :laugh: ...I still five'd the answer since it would probably work.

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

            It will work, Windows uses it all over the place for the ASCII/Unicode versions of function names.

            Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

            A 1 Reply Last reply
            0
            • A Albert Holguin

              The obvious way would be:

              class cl
              {
              public: //or private, but with your example above,
              // the external interface for a class should be public
              CString short_l(int i){ return FunctionWithADisturbingLongName(i);}
              }

              Now, this has the added clarity of still allowing the compiler to point you in the correct direction for debugging errors. Using a macro would probably switch names on you all the time (if there's an error it'll report the substituted string).

              C Offline
              C Offline
              Chuck OToole
              wrote on last edited by
              #11

              Stick an __inline in there and that ought to do it.

              class cl
              {
              public: //or private, but with your example above,
              // the external interface for a class should be public
              __inline CString short_l(int i){ return FunctionWithADisturbingLongName(i);}
              }

              1 Reply Last reply
              0
              • M Maximilien

                Me think that it is a bad idea. Your code will be peppered with function calls to short and/or abbreviated method names that will not reflect what it actually do; and the user (other developers) will need to look at the documentation to know what it should be doing.

                Watched code never compiles.

                C Offline
                C Offline
                Chuck OToole
                wrote on last edited by
                #12

                oh, I agree that this would be bad practice. Basically, you have a descriptive function name that makes the code reading for understanding far easier. But instead, you'll have code that uses short, cryptic names because it's easier to type (unless you're letting the wizard auto-complete function names) There's a reason that .NET Obfuscation routines create names like "N1", "N2", etc. It's because it makes the code harder to read and understand. Why do that on purpose to code you have to maintain.

                A 1 Reply Last reply
                0
                • L Lost User

                  It will work, Windows uses it all over the place for the ASCII/Unicode versions of function names.

                  Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                  A Offline
                  A Offline
                  Albert Holguin
                  wrote on last edited by
                  #13

                  I wasn't doubting it. :)

                  L 1 Reply Last reply
                  0
                  • C Chuck OToole

                    oh, I agree that this would be bad practice. Basically, you have a descriptive function name that makes the code reading for understanding far easier. But instead, you'll have code that uses short, cryptic names because it's easier to type (unless you're letting the wizard auto-complete function names) There's a reason that .NET Obfuscation routines create names like "N1", "N2", etc. It's because it makes the code harder to read and understand. Why do that on purpose to code you have to maintain.

                    A Offline
                    A Offline
                    Albert Holguin
                    wrote on last edited by
                    #14

                    Agree too... unless the short names still provide sufficient information to make it readable, although if that's the case... why keep the long names at all (unless it's a library of sorts)...

                    1 Reply Last reply
                    0
                    • M Maximilien

                      Me think that it is a bad idea. Your code will be peppered with function calls to short and/or abbreviated method names that will not reflect what it actually do; and the user (other developers) will need to look at the documentation to know what it should be doing.

                      Watched code never compiles.

                      J Offline
                      J Offline
                      jschell
                      wrote on last edited by
                      #15

                      I too agree with all of that.

                      1 Reply Last reply
                      0
                      • A Albert Holguin

                        I wasn't doubting it. :)

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

                        The word probably implied that you were not sure. :~

                        Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman

                        1 Reply Last reply
                        0
                        • J Joschwenk666

                          Hi, I´m searching for the best way, to define some kind of as function alias in C++ I have the following class:

                          class c1
                          {
                          private:
                          CString FunctionWithADisturbingLongName(int i);
                          }

                          And i want the function to have the long function name, because the function of the function has to be explained in it :). But I want to have a shortcut to the function. I want to do this

                          c1 test;
                          test.short_1(1);

                          instead of

                          c1 test;
                          test.FunctionWithADisturbingLongName(1);

                          Of course I could declare and define short_1 as a member of c1, and then call FunctionWithADisturbingLongName in it, but there has to be a better solution. Thank you for helping!

                          S Offline
                          S Offline
                          Stefan_Lang
                          wrote on last edited by
                          #17

                          There are several ways 1. #define works, if you choose a reasonably unique name, but it clutters the global namespace and therefore not a good idea. 2. declaring a second function that wraps the first adds code cluttering in your original class 3. a function pointer could work, but involves awkward syntax both for defining the pointer and calling the function 4. a function object would add quite a lot of code, but requires neither changing your original class nor dereferencing to invoke the function:

                          class shortname {
                          public:
                          shortname(class c1* p) : pclass(p) {}
                          CString operator()(int i) { return pclass->FunctionWithADisturbingLongName(i); }
                          private:
                          class c1* pclass;
                          };

                          void foo() {
                          c1 test;
                          shortname(&test);
                          CString result = shortname(1);
                          }

                          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