Thread: call function

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    2

    call function

    hello guys.
    i want to func2 could determine that which function has been called it.as i explain in comment in func2 in the below. any help would be appreciated


    Code:
    #include <iostream>
    using namespace std;
    void func2(void)
    {
        //how can i get the function name that call func2
        //and also the line that func2 called in that
        //and imagine there is more than one file in my project
        //then how can i find which file and line and function called
        //func2 . the real question is i can not complete func2
        // in a way that could figure out what function call this function
    }
    int func1(int a, int b)
    {
        if(a>b)
           func2();
        else
           cout << "a+b = " << a+b;
    }
    void func3(void)
    {
        func2();
    }
    Last edited by fics_1991; 09-12-2014 at 05:03 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The obvious approach would be to pass something into func2 - a string perhaps - from the calling function that indicates who called it.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If you want a solution that works portably (i.e. with all implementations that comply with the C++ standard) you will need cooperation of the caller - such as passing a string like hk_mp5kpdw suggests.

    If you want to do things like debuggers do, then you are reliant on implementation-specific behaviours. In that case, you will need to ask in a forum for programming on your target system, with you compiler of choice.

    More generally, however, in what form do you want the "function name"? What do you expect func2() to do with the "function name" it receives?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why would you want to do it anyway?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Sep 2014
    Posts
    2
    thanks dude

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    What operating system and compiler? What you're probably looking for is called a stack trace. I've done this on Linux with GCC, and the process is (mostly) the same with GCC on any platform. You would use a combination of the backtrace(), backtrace_symbols(), and abi::__cxa_demangle(), along with a few compiler command line options. I don't know what you would do for MS Visual C++, but hopefully, this gets you somewhere. Google can help you further.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function call Overhead and Function Call Stack
    By Alam Khan in forum C++ Programming
    Replies: 2
    Last Post: 04-26-2014, 08:28 AM
  2. Function Prototype, Function Call, and Function definition
    By dmcarpenter in forum C Programming
    Replies: 9
    Last Post: 04-09-2013, 03:29 AM
  3. Replies: 3
    Last Post: 09-22-2011, 09:59 PM
  4. Replies: 8
    Last Post: 07-08-2011, 01:16 PM
  5. Replies: 5
    Last Post: 10-17-2006, 08:54 AM