How do I do this.....

This is a discussion on How do I do this..... within the C++ Programming forums, part of the General Programming Boards category; I am a newbie so please be easy on me I have C file...which has following for an example. Code: ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,750
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,750
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,750
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21