bothersome externs

This is a discussion on bothersome externs within the C++ Programming forums, part of the General Programming Boards category; I have been writing a simple comsole RPG game. I have my characters in a characters.cpp file, using a class ...

  1. #1
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281

    Question bothersome externs

    I have been writing a simple comsole RPG game. I have my characters in a characters.cpp file, using a class called creature. I need to be able to declare the variables for my creatures, say the vulture (object of creature) so i can access it's methods through engine.cpp (my main file). If anyone has any tips for this situation, it would be appreciated, and if anyone knows where i can find a tutorial on using extern and making header files and such, let me know! Thanks in advance!

    ~Inquirer

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Avoid globals if possible. Why not have a non-global instance of the creatures somewhere within the engine?
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Sometimes globals are necessary. You have an 'engine' - it should be global, right?

    On a slightly different, but related note:

    I wish someone could explain to me what the difference is between using a static and an extern in the following case:

    static int gv_globalCount;

    or

    extern int gv_globalCount;
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I wish someone could explain to me what the difference is between using a static and an extern in the following case

    static int gv_globalCount; - This means that gv_globalCount is an integer that can only be accessed inside the source file that it was declared in. So if it is declared in foo.cpp then it cannot be used in bar.cpp.

    extern int gv_globalCount;- This is the exact opposite, it means that gv_globalCount is defined elsewhere but you can use it here. If it was defined in foo.cpp then bar.cpp could declare it as extern and use it.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    externs are necessary in most projects. just don't make ALL of the variables global, some n00bs have been known to do this.

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    globals are never "necessary" but not entirely evil for something like an engine object. Engines are after all sets of global functions. Although, your app is only alive for the time that main() is alive. therefore the engine could exist there and be passed into other objects. I've begun practicing the same thing with HINSTANCE of the app because it was the only global I was still using. I am now global free and much happier
    always looking, make an offer. get me out of this place.

  7. #7
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140

    Dont worry! Green Day is here!

    I'm doing the exact same thing Inquirer. Download this .zip and see what I did. All my classes are in classes.h, and my main source-code (sourceCode.cpp) uses objects that are created in classes.h.
    Attached Files Attached Files
    AOL: GrNxxDaY
    IDE: Dev-C++ Beta 5 (v4.9.4.1)
    Project: Eye of Sahjz (text-RPG)
    If you think I may need help, please IM me.

  8. #8
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    THanks guys! All the stuff you guys posted has combined to make my test run of my program successful. As soon as i am finished with a few rooms and the fight module of the engine, i will post int in the games forum.

    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Externs, includes, protection OH MY
    By Inquirer in forum Game Programming
    Replies: 10
    Last Post: 10-15-2002, 03:05 PM
  2. fgets and a bothersome newline char
    By ivandn in forum Linux Programming
    Replies: 1
    Last Post: 11-14-2001, 12:41 PM

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