C++ Promblem

Discussion in 'Indie Basics' started by ProgrammingFreak, Apr 23, 2006.

  1. ProgrammingFreak

    Original Member

    Joined:
    Oct 22, 2005
    Messages:
    126
    Likes Received:
    0
    I am having trouble understadning this programming concept

    Example:
    typedef struct{
    int Helath;
    int Armor;
    }b_stuff;

    Yes that is an example of it, I understand how to do that, but how do you give the variables vaules after they have declared in there, I tried it out of there and I got an error, I tried it in the array and got an error, any help
     
  2. RoadMaster

    Original Member

    Joined:
    Apr 2, 2006
    Messages:
    102
    Likes Received:
    0
    Edit: Removed because others have answered your question better, and I clearly need to re-read some of my own C++ books as I've forgotten the differences between C and C++, dur...
     
    #2 RoadMaster, Apr 23, 2006
    Last edited: Apr 23, 2006
  3. svero

    Moderator Original Member Indie Author

    Joined:
    Jul 27, 2004
    Messages:
    3,392
    Likes Received:
    6
    There's two concepts here.. the typedef and the struct

    The typedef is just a way of assigning a new name to some type when it's more convenient or if you're having trouble withe the pre-processor (#define's don't work sometimes with pointer types)

    So instead of... Sprite* you could write...

    typedef Sprite* PSprite;

    and then define a function that takes a PSprite (pointer to sprite)

    --

    Now the struct is something else.. That's a way of grouping things together that belong together. Here's a useful example...

    Code:
    struct PLAYER
    {
    int Lives;
    int Level;
    int points;
    };
    
    theres a basic struct that has all the information about a player in a basic arcade game. What level they're on, how many lives they have left, and how many points they've accumulated. Then somewhere else in the code you might do...

    Code:
    PLAYER player1;
    PLAYER player2;
    
    to create 2 actual players. Or alternately...

    Code:
    PLAYER Players[2];
    
    and then in the game where the player gets some points...

    Code:
    Players[CurrPlayer].points +=250;
    
    etc...

    A small note is that a struct is equivalent to a class in C++ where everything is public by default.. so these are the same..

    Code:
    struct A
    {
    int x;
    int y;
    };
    
    class A
    {
    public :
    int x;
    int y;
    };
    
     
  4. ggambett

    Moderator Original Member Indie Author

    Joined:
    Jul 26, 2004
    Messages:
    1,982
    Likes Received:
    6
    You can also init static struct data this way

    static b_stuff lStuffs[] = { { 1, 2, 3 }, { 4, 5, 6 } };

    This creates an array of two b_stuff structs, the first with 1 life, level 2 and 3 points, and the other with values 4, 5 and 6.

    Or create a constructor (the only difference between structs and classes in C++ is that classes are private by default and structs are public).

    b_stuff::b_stuff (int nLives, int nLevel, int nPoints) : nLives(nLives), nLevel(nLevel), nPoints(nPoints) {}

    You can use it this way

    b_stuff pStuff1(1, 2, 3);

    or

    b_stuff* pStuff2 = new b_sStuff(1, 2, 3);
     
  5. mahlzeit

    Original Member

    Joined:
    Sep 26, 2004
    Messages:
    852
    Likes Received:
    1
    Small note: There is a difference between C and C++ here. In C, you do need the typedef but in C++ you don't. (Although they may have lifted that restriction in a recent revision of the language specs: the last time I used pure .c files was ages ago.) Anyway, if you look at old sources that may be confusing a little.
     
  6. ProgrammingFreak

    Original Member

    Joined:
    Oct 22, 2005
    Messages:
    126
    Likes Received:
    0
    Ok I get it kinda
     
  7. dmikesell

    Original Member

    Joined:
    Jan 10, 2006
    Messages:
    146
    Likes Received:
    0
    You may either want to start with a simpler language (Python, Ruby, etc.) or consider a simpler application than a game with which to learn C++.
     

Share This Page

  • About Indie Gamer

    When the original Dexterity Forums closed in 2004, Indie Gamer was born and a diverse community has grown out of a passion for creating great games. Here you will find over 10 years of in-depth discussion on game design, the business of game development, and marketing/sales. Indie Gamer also provides a friendly place to meet up with other Developers, Artists, Composers and Writers.
  • Buy us a beer!

    Indie Gamer is delicately held together by a single poor bastard who thankfully gets help from various community volunteers. If you frequent this site or have found value in something you've learned here, help keep the site running by donating a few dollars (for beer of course)!

    Sure, I'll Buy You a Beer