Thread: Making global inside a block

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246

    Making global inside a block

    How can I define global arrays, vars, etc inside a function? I want to access these objects from other .cpp files.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    Registered User Kurisu's Avatar
    Join Date
    Feb 2006
    Posts
    62
    Why are you trying to define them inside a function? If they are global than why not declare them at the top of the file?
    Last edited by Kurisu; 02-06-2006 at 05:50 AM. Reason: Curious if signature is working.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    How can I define global arrays, vars, etc inside a function?
    Don't ever declare global variables. If a function needs a variable, then pass it to the function.

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    >>Why are you trying to define them inside a function? If they are global than why not declare them at the top of the file?

    I want to put a function in a .lib that should define some objects. Other functions in that lib and even other .cpp files that had included the header of the lib should be able to use these objects.


    >>Don't ever declare global variables. If a function needs a variable, then pass it to the function.

    Objects should be defined in a function, but the caller of functions which use those objects is not that.
    Last edited by siavoshkc; 02-06-2006 at 07:13 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Forum displayed wrong last post.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    Objects should be defined in a function, but the caller of functions which use those objects is not that.
    The caller is also a function...

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I mean the "caller function" is not the "definer function". You think I don't know that caller is function?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  8. #8
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    My question was simple guys. Defining Global inside a block.
    Is there any way to do so, or not?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    No. You cannot define a global inside a block. By definition, a global is a variable that's defined outside any block.

    It was possible, however, at least in C, to declare globals inside a block, using the extern keyword. Not sure if that's still legal in C++, it might be one of the places where the language was changed.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I don't know C but in C++, extern tells compiler "This object will be defined somewhere else so don't allocate any space to them in memory". And it's not what I want.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yes, it makes a declaration instead of a definition. As I said, it is not possible to have a global definition inside a block.

    Not that there is any compelling reason for having this. Globals are global, and attempting to define them inside blocks therefore nonsense.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Note that while you can't define a variable with global scope in a function block, you can define a variable with global lifetime by using the static qualifier.

  13. #13
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I know, but as I said all of my functions should have access to objects. I know the best solution for my problem is using a class, but I don't want to do so.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  14. #14
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    have the function return the objects. having that kind of random global access to local variables is a receipe for disaster and the very concept of it should deeply offend you.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I still don't get where you see a difference between what you want to do and normal global variables. What's so difficult about writing a header and a source file?

    globals.h
    Code:
    #ifndef GLOBALS_H_LIGJJSAIEWJRKADLKE
    #define GLOBALS_H_LIGJJSAIEWJRKADLKE
    
    extern int global_number_one_with_big_ugly_name_to_discourage_you_from_using_it;
    extern int global_number_two_with_big_ugly_name_to_discourage_you_from_using_it;
    
    #endif
    globals.cpp
    Code:
    #include "globals.h"
    
    int global_number_one_with_big_ugly_name_to_discourage_you_from_using_it = 1;
    int global_number_two_with_big_ugly_name_to_discourage_you_from_using_it = 2;
    What would the semantic differences of whatever you have in mind be?

    (Edit: Don't mind the spaces in the names, they're inserted by the board.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making a wstring variable global scope
    By stanlvw in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2008, 02:25 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. My memory management solution
    By cboard_member in forum Game Programming
    Replies: 20
    Last Post: 08-23-2006, 09:07 AM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  5. definition inside block
    By PutoAmo in forum C Programming
    Replies: 13
    Last Post: 04-19-2002, 09:20 AM