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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to get function name?

How to get function name?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
7 Posts 7 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.
  • H Offline
    H Offline
    hanlei0000000009
    wrote on last edited by
    #1

    Thanks all!

    void fun()
    {
    string str;

    str = ???; //How to get the function("call") name which is call this function?

    cout << str << endl; // I want output "call"
    }

    void call()
    {
    fun();
    }

    modified on Wednesday, February 25, 2009 2:19 AM

    _ C S 3 Replies Last reply
    0
    • H hanlei0000000009

      Thanks all!

      void fun()
      {
      string str;

      str = ???; //How to get the function("call") name which is call this function?

      cout << str << endl; // I want output "call"
      }

      void call()
      {
      fun();
      }

      modified on Wednesday, February 25, 2009 2:19 AM

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      The __FUNCTION__ macro will give you the function name.

      «_Superman_» I love work. It gives me something to do between weekends.

      I CPalliniC 2 Replies Last reply
      0
      • _ _Superman_

        The __FUNCTION__ macro will give you the function name.

        «_Superman_» I love work. It gives me something to do between weekends.

        I Offline
        I Offline
        Iain Clarke Warrior Programmer
        wrote on last edited by
        #3

        I've just learned something today - and it's before 9am! I can now close my mind to further information, as I have to ration these things. Iain.

        Codeproject MVP for C++, I can't believe it's for my lounge posts...

        H 1 Reply Last reply
        0
        • _ _Superman_

          The __FUNCTION__ macro will give you the function name.

          «_Superman_» I love work. It gives me something to do between weekends.

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          I suppose __FUNCTION__ would expand to "foo" while he's looking for "call" (i.e. the caller function name). :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          1 Reply Last reply
          0
          • I Iain Clarke Warrior Programmer

            I've just learned something today - and it's before 9am! I can now close my mind to further information, as I have to ration these things. Iain.

            Codeproject MVP for C++, I can't believe it's for my lounge posts...

            H Offline
            H Offline
            Hamid Taebi
            wrote on last edited by
            #5

            You can see more info about macro's(LINE,..) on the www.functionx.com and www.cplusplus.com. ;)

            Of one Essence is the human race thus has Creation put the base One Limb impacted is sufficient For all Others to feel the Mace (Saadi )

            1 Reply Last reply
            0
            • H hanlei0000000009

              Thanks all!

              void fun()
              {
              string str;

              str = ???; //How to get the function("call") name which is call this function?

              cout << str << endl; // I want output "call"
              }

              void call()
              {
              fun();
              }

              modified on Wednesday, February 25, 2009 2:19 AM

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

              Check this[^] out, but note that -as far as i know- these only work with debug information present, but i might be wrong.

              > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Life: great graphics, but the gameplay sux. <

              1 Reply Last reply
              0
              • H hanlei0000000009

                Thanks all!

                void fun()
                {
                string str;

                str = ???; //How to get the function("call") name which is call this function?

                cout << str << endl; // I want output "call"
                }

                void call()
                {
                fun();
                }

                modified on Wednesday, February 25, 2009 2:19 AM

                S Offline
                S Offline
                Stuart Dootson
                wrote on last edited by
                #7

                The easiest way is to require the calling function to pass in its name, which it can get with __FUNCTION__:

                void fun(const char* callerName)
                {
                string str;

                str = callerName;

                cout << str << endl; // I want output "call"
                }

                void call()
                {
                fun(__FUNCTION__);
                }

                Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                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