how to show my class a global variable

This is a discussion on how to show my class a global variable within the C++ Programming forums, part of the General Programming Boards category; ok sorry for the silly post name but it does explain what i need help with. im pretty sure that ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    118

    how to show my class a global variable

    ok sorry for the silly post name but it does explain what i need help with.
    im pretty sure that i need to use a pointer but i get errors and crashes when i try to use them.

    i have a main program which includes a few classes i made for a basic(very basic) blackjack game.

    i have a funtion called twist
    Code:
    void player::twist(card drawn)
    {
    	myHand[cardcount]= drawn;
    	cardcount++;
    }
    is there some way i can make only one version of cardcount when i make
    player player1; player player2;
    because if i deal 2 cards each with the current way it deals them the same cards
    im sure if i made cardcount a pointer to a global cardcount then it would work but i dont seem to be able to i have a theory that i may be able to use "extern"
    but im not sure what that is or if it would help me.
    thanks in advanced im sure i didnt include the right info you will need so feel free to give me a nudge
    oh i think i could include a file which has the cardcount to my player class and main program but dont know if thats the best way i mean a whole file for one int?

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Are you saying there should be one instance of cardcount for every instance of player?

    Make it a static data member.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    yes i think thats what i need i want to make cardcount as 1 global int across the many player objects i make im not sure how to use static data members but ill look it up quick thanks for the help

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Thinking about the problem domain more, maybe you want to create a class called "hand" or "dealer" or "game" that maintains the card count, and then have players hold a reference or pointer to that object.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    118
    that sounds more like what ive been trying to do except that im a novice with pointers and get lots of errors and crashes .
    ill try again with pointers and see if i can get it working

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class read as variable, default-type int? Say what?!
    By arcaine01 in forum C++ Programming
    Replies: 8
    Last Post: 07-10-2009, 06:34 PM
  2. Static Local Variable vs. Global Variable
    By arpsmack in forum C Programming
    Replies: 7
    Last Post: 08-21-2008, 04:35 AM
  3. Static global variable acting as global variable?
    By Visu in forum C Programming
    Replies: 2
    Last Post: 07-20-2004, 09:46 AM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

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