Results 1 to 26 of 26

Thread: global variables

  1. #1

    Thread Starter
    Addicted Member DarkMoose's Avatar
    Join Date
    Jul 2000
    Location
    in a box
    Posts
    185

    global variables

    how do you declare global variables in c++ that can be used in any cpp file in the project?
    To understand recursion, one must first understand the concept of recursion.

  2. #2
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    uhh.... declare it in a header file

  3. #3
    Hyperactive Member sw_is_great's Avatar
    Join Date
    Nov 2003
    Posts
    330
    #include <stdio.h>

    int a; /*This is a global variable */

    main()
    {}
    Regards

  4. #4
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    And, for another cpp file, redeclare the variable, with 'extern' before it.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  5. #5

    Thread Starter
    Addicted Member DarkMoose's Avatar
    Join Date
    Jul 2000
    Location
    in a box
    Posts
    185
    Originally posted by sw_is_great
    #include <stdio.h>

    int a; /*This is a global variable */

    main()
    {}
    no that's just for one module
    To understand recursion, one must first understand the concept of recursion.

  6. #6

    Thread Starter
    Addicted Member DarkMoose's Avatar
    Join Date
    Jul 2000
    Location
    in a box
    Posts
    185
    when i do the extern part, i get an 'already defined' error
    To understand recursion, one must first understand the concept of recursion.

  7. #7
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    Don't put the variable in the header (unless the header's being included in all cpps). Just put it in oue of the cpps. With extern in all the others.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    What SLH said, with one addition.

    If you put:
    Code:
    int g_myvar;
    ...into a source file, you can put:
    Code:
    extern int g_myvar;
    ...into a header, which is included by all the other source files.

    If you find yourself having a lot of global variables (you shouldn't really have *any* but I suppose sometimes you have to break the rules), put them all in one .c or .cpp file, with an associated header.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    nothign wrong with global variables.. thats just oop nonsense, you can follow those rules if you are an oop fan, but C++ is not just for oop
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    ...but it just makes your code a hell of a lot easier to read if you
    declare local variables where they're needed instead of having
    globals.

    Besides, what if you have a global that several functions
    manipulate? If you have a big program, why would you want to
    keep track of what ten or so functions manipulate a global and
    when?

  11. #11
    Member
    Join Date
    Jun 2003
    Posts
    43

    Smile

    hi,
    For using a variable throught your project, declare it in a header

    file and include the file wherever required.
    The redefinition error is coming because of including a file more than once.
    For avoiding this use macros like

    #ifdef #def #ifndef #else

    try this...

  12. #12
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Originally posted by kedaman
    nothign wrong with global variables.. thats just oop nonsense, you can follow those rules if you are an oop fan, but C++ is not just for oop
    There is a problem with global variables. They can be used anywhere in anything. That is pretty dangerous.

    Also, global variables have absolutely nothing to do with oop, so I got no idea why you mentioned it.

  13. #13
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    Originally posted by kasracer
    There is a problem with global variables. They can be used anywhere in anything. That is pretty dangerous.
    I totally agree.

    Originally posted by kasracer
    Also, global variables have absolutely nothing to do with oop, so I got no idea why you mentioned it.
    I disagree here. Part of OOP is making a class or 'object'
    for everything so you don't have to depend on globals.

    Encapsulation, in theory, treats objects as 'black boxes' where
    the data members and inner workings of the class are basically
    invisible.

    I'm not saying you CAN'T mix globals and OOP but the idea of OOP
    is to do away with them.

  14. #14
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Originally posted by wey97
    I disagree here. Part of OOP is making a class or 'object'
    for everything so you don't have to depend on globals.

    Encapsulation, in theory, treats objects as 'black boxes' where
    the data members and inner workings of the class are basically
    invisible.

    I'm not saying you CAN'T mix globals and OOP but the idea of OOP
    is to do away with them.
    Ugh, global variables don't have anything to do with OOP. OOP can be used to help eliminate them, but if a class eliminates a global variable, then there was no reason it should have been a global variable in the first place and could have been treated different.

    Global variables are accessable everywhere. The fact that C++ has OOP doesn't change anything about global variables.

    OOP creates a better way of managing a program for the programmer. It doesn't eliminate global variables in its nature. If OOP itself eliminates your global variables, then you probably need to re-think your programs design/structure.

  15. #15
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    There is a problem with global variables. They can be used anywhere in anything. That is pretty dangerous.
    Dangerous for OOP programmers maybe, but anyone fairly experienced procedural programmer has the sense to avoid it. Global variables are there for a reason, and if you compare to java which just OOP and not multiparadigm like C++, there are no global variables at all.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  16. #16
    Hyperactive Member Comreak's Avatar
    Join Date
    Feb 2001
    Location
    Dis
    Posts
    319
    Originally posted by kedaman
    Dangerous for OOP programmers maybe, but anyone fairly experienced procedural programmer has the sense to avoid it.
    Isn't it a good idea to eliminate the risk anyway? It's like keeping a loaded gun with the safety off because your sure nobody is going to touch it.


    Global variables are there for a reason, and if you compare to java which just OOP and not multiparadigm like C++, there are no global variables at all.
    Aren't global variables just left overs from C (C++ being a superset of C and all)?
    C.O.M.R.E.A.K.: Cybernetic Obedient Machine Responsible for Exploration and Accurate Killing

  17. #17
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    yes, and C is procedural. Eliminate the risk and eliminate its uses.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  18. #18
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Even in C globals were kept to the minimum.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  19. #19
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    Originally posted by kasracer
    Ugh, global variables don't have anything to do with OOP. OOP can be used to help eliminate them, but if a class eliminates a global variable, then there was no reason it should have been a global variable in the first place and could have been treated different.

    Global variables are accessable everywhere. The fact that C++ has OOP doesn't change anything about global variables.

    OOP creates a better way of managing a program for the programmer. It doesn't eliminate global variables in its nature. If OOP itself eliminates your global variables, then you probably need to re-think your programs design/structure.
    C++ is not complete OOP. That's why you can still have globals
    and I can see cases where you might still need them or where it's
    more useful to have them.

    You're still missing the point.

    Part of OOP is doing away with globals.
    Why do you think there are no globals at all in Java?

    That's because it's almost complete OOP.

  20. #20
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Originally posted by wey97
    Part of OOP is doing away with globals.
    No it isn't
    Originally posted by wey97

    Why do you think there are no globals at all in Java?
    You can use globals in Java as you can in C++......

  21. #21
    Member
    Join Date
    Oct 2003
    Location
    England
    Posts
    40

    So how are we supposed to do it then?

    Ok this is something that has baffled me for a while. Why not use global variables and what are the alternatives. I use global variables only when i need to but if there was a way round it that was safer then i would use it.

    One of my current projects requires me to have a list of questions and i am using a STL vector to hold them all. This list needs to be accessed throughout my form (borland C++ builder) so how can i declare this list that is accessed in different functions to save/load/edit the list.

    I have been told many times not to use global variables but i have never been told the alternative

    Thanks

  22. #22
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: So how are we supposed to do it then?

    Originally posted by Greenslime
    One of my current projects requires me to have a list of questions and i am using a STL vector to hold them all. This list needs to be accessed throughout my form (borland C++ builder) so how can i declare this list that is accessed in different functions to save/load/edit the list.

    I have been told many times not to use global variables but i have never been told the alternative

    Thanks
    Pass by reference, pointers

  23. #23
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Especially easy, just make it a member of the form class.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  24. #24
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    Originally posted by kasracer
    No it isn't
    You can use globals in Java as you can in C++......
    You're wrong, buddy.

    http://www.developer.com/java/other/article.php/626151


    Java and C++ are quite similar, and this makes it relatively easy for C++ programmers to learn the newer language. There are a few areas, however, that are different enough to cause problems, and one of these is the lack of global variables.

  25. #25
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Originally posted by wey97
    You're wrong, buddy.

    http://www.developer.com/java/other/article.php/626151
    I don't know much of Java, but I can use global variables in it just like I can with C++

    Maybe it means they can't be used across source files?

  26. #26
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    There are no global variables in Java, but you can give a class public static variables, the result is the same.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width