Thread: Need a little help with lists in a Class

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    3

    Need a little help with lists in a Class

    Ok, I just need a little help. I remember trying to make a list for another Class a few months back for a game I was making for a school assignment, and the list didn't want to initialize. I can't test this yet, as my program isn't done yet, but I just wanted to see if my code so far was correct.

    The first part(cut down for clarity):

    Code:
    private:
    
      int maxhp;
      int fightermaxhplist[50] = { 35, 63, 91, 120, 149, 178, 207, 237, 267, 297, 327, 333, 364, 395, 401, 433, 465, 472, 504, 537, 545, 578, 611, 620, 629, 663, 672, 682, 717, 727, 737, 773, 784, 795, 831, 843, 855, 892, 904, 917, 955, 968, 981, 1020, 1034, 1048, 1087, 1102, 1117, 1157};
    The second part:

    Code:
    int charstats::calculatemaxhp()
      {
        if((charclass==1) || (charclass ==7))
        {
          maxhp = fightermaxhplist[level-1];
        }
    Will that work, or will I have to initialize the list some other way? I'd like to keep the list in the class if that's possible, though I could work it into the main program if I had to.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You cannot init data directly in the class, that's what the constructor is for.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    3
    Well, that kinda sucks. Thanks for the reply, though. Guess I'll have to rework my class, and put that into my main program.

  4. #4
    Registered User Nippashish's Avatar
    Join Date
    Sep 2002
    Posts
    34
    You can use a pointer as long as you remember to free up the memory when the object is destroyed.
    Code:
    class Fighter
    {
    public:
    	Fighter();
    	~Fighter() { delete [] _maxHpList; }
    private:
    	int *_maxHpList;
    };
    
    Fighter::Fighter()
    {
    	_maxHpList = new int[50];
    
    	// assign your values here. I dont know any way of just saying maxHpList = { ... }
    	// if you're doing it this way, so it would help if you had a formula.
    }

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    3
    Don't I know it. Unfortunately, I don't know the exact formula, though I think I could come up with one if I really needed to. I just made it into a funtion in my main file, and it works fine now.

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    If the array is going to be the same for each class produced and wont be altered, then you could make it a const static

    Code:
    #include <iostream>
    
    
    class foobar{
    private:
    	static const int Array[10];
    public:
    	int operator[](int n){return Array[n];}
    };
    
    const int foobar::Array[10] = {6,2,5,5,4,5,6,3,7,6};
    
    int main(void){
    
    	foobar f;
    
    	for(int i = 0; i < 10; ++i)
    		std::cout << f[i] << " ";
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 04:30 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM