can you make a variable in a function in the calling functions scope?

This is a discussion on can you make a variable in a function in the calling functions scope? within the C++ Programming forums, part of the General Programming Boards category; i have a function that initiates the engine for my project, and i want to put everything that deals with ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    137

    can you make a variable in a function in the calling functions scope?

    i have a function that initiates the engine for my project, and i want to put everything that deals with initiating the project in it. that includes the variables. its not a big deal if i can't do it, because there really isn't a big need to do it. i just think it will make the code cleaner. is there a way that i could do something like this:

    PHP Code:
    void init()
    {
        
    int p 10;
    }

    int main()
    {
        
    init();

        
    cout << p;

        return 
    0;


  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Consider passing parameters to the function.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    well if i pass parameters then i still have to declare the variable in main, which is what i don't want to do.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    If the scope of the variables is to be in main, so must the variables. Can you package your variables in a class and handle such things in the constructor? And then access the variables via accessor functions?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    i could, but i just wanted to do it for clearity. i don't really think its worth any work when i could leave it how it is. thank you for your time.

  6. #6
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Clarity? possibly a namespace.

    Code:
    namespace init
    {
        int p = 10;
    }
    
    int main()
    {
        cout << init::p;
    
        return 0;
    }
    If you wanted it exactly like your example you could make it a using declaration, although thats somewhat pointless.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 04:33 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. error LNK2001: unresolved external symbol
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 07-12-2002, 09:45 PM

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