Thread: How do I do this.....

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    10

    How do I do this.....

    I am a newbie so please be easy on me

    I have C file...which has following for an example.
    Code:
    extern int globalvar;
    
    void init()
    {
       While (something)
       {
          x++
        }
        globalvar = x;
    }
    
    int getglobalvar (void)
    {
       return(globalvar);
    }
    Now I have a C++ file.....

    Which I have a function

    Code:
    void needGlobalvar()
    {
      int y = getglobalvar() 
    }
    basically I will be then calling this function needGlobalvar in another C++ else....whats the best way I could move the function
    Code:
    int getglobalvar (void)
    {
       return(globalvar);
    }
    to C++ and still have the access to correct information set in the C function for the globalvar

    One of my friends said to make the globalvar a singleton and access it so this way it will be safe....could someone please help me with how I could use the sigleton...because I have not done that before.....

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Somewhere in your C++ code, before you try to call the function, you need
    Code:
    extern "C" int getglobalvar (void);
    Then you just compile everything, making sure you compile main() using C++, and you should be good.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    10
    Thanks Salem,

    Could you please kindly....show me the flow with this change? It will help me to learn and understand...

    Thanks much.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    There isn't anything else to show - that's it, just that one line, placed at a suitable point in your C++ code (perhaps an included header file).

    This is good information, if you're mixing C and C++.
    [32] How to mix C and C++ Updated! , C++ FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    10
    Quote Originally Posted by Salem View Post
    There isn't anything else to show - that's it, just that one line, placed at a suitable point in your C++ code (perhaps an included header file).

    This is good information, if you're mixing C and C++.
    [32] How to mix C and C++ ..Updated!.., C++ FAQ
    Salem again...thanks.....
    But I would like to move the

    Code:
    int getglobalvar (void)
    {
       return(globalvar);
    }
    to C++ and make it a C++ function....

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Hey, it's entirely up to you.

    Either you do extern "C" on a function of your choice, or you do extern "C" on the global variable.

    I don't understand why you haven't actually tried this for yourself yet, rather than trying to get me to do it for you (not going to happen).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed