Question for class- & array-savvy people

This is a discussion on Question for class- & array-savvy people within the C++ Programming forums, part of the General Programming Boards category; (please tell me if this doesn't make sense) First, a little background I initialized an array of classes using this: ...

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

    Arrow Please help?

    (please tell me if this doesn't make sense)
    First, a little background
    I initialized an array of classes using this:
    Code:
    Room roomArray[20][30];   //class declaration already done, dont worry 'bout that
    Works great.

    Then i created a function that initializes the elements with the values i give... like this...
    Code:
     Room temp1(3, 5, false, true, false, true, "A Dirt Trail", "Very dusty");
     roomArray[3][5] = temp1;
    
     Room temp2(3, 6, false, false, false, true, "A Dirty Trail", "whatever");
     roomArray[3][6] = temp2;
    1) Is there a way to initialize an element of the array without making that temp object?
    I tried doing this...
    Code:
    roomArray[3][5](3, 5, false, true, false, true, "Dirty Trail", "blabla")  // doesn't work
    2) Why doesn't that work?

    Also, i can't initialize other elements without creating new temporary variables (e.g., Room temp1; Room temp2
    Doing this will make the RAM needed to create all those Room objects double.
    3) is there a better way?
    (won't let me re-initialize the temp object like this..)
    Code:
    Room temp(arguments);
    roomArray[3][5] = temp;
    
    temp(new arguments);
    roomArray[3][6] = temp;
    Last edited by GrNxxDaY; 07-25-2002 at 11:44 PM.
    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.

  2. #2
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    use a function to do it:

    Room rooms[ 10 ];
    rooms.setValues( "bedroom", 12, 12, other data member initialisation... )


    Or you could dynamically allocate your 2d array and use the constructor. This is a more complicated method, so I'll leave someone else to show you how to dynamically create a 2d array

  3. #3
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    can you elaborate on that? look's like a member function - but can't tell much from it
    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.

  4. #4
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    it is, a more complete example:
    Code:
    class Room
    {
    public:
       Room( );
       Room( char* name, int width, int length );
       ~Room( );
    
       bool setValues( char* name, int width, int length );
    
    private:
       char* roomName;
       int roomWidth, roomLength;
    };
    
    
    bool Room::setValues( char* name, int width, int length )
    {
       if( width <= 0 || height <= 0 )
       {
          return false;  //invalid room dimensions
       }
       else
       {
          roomWidth = width;
          roomLength = length;
       }
    
       roomName = new char[ strlen( name ) + 1 ];  //done :)
       roomName = name;
    }
    A similar system is used in MFC, except they call their functions Create( ... )
    Last edited by endo; 07-26-2002 at 05:01 AM.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    If you are dynamically allocating memory like the previous example, make sure to add 1 to the string length for the null terminator.

  6. #6
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    Ahh, I think the problem is solved. I used endo's first idea.. sorta.
    Here's what I did, if anyone else has this problem

    I created a member function similar to a constructor function, that takes arguments and sets the data members equal to them. Then when I want to create a room, this is what I do...

    Code:
    // a shortened version
    Room::initialize(int px, int py, char* pdesc, char* pname)
    {
     x = px; y = py; desc = pdesc; name = pname;
    }
    
    void createRooms()
    {
     roomArray[3][4].initialize(3, 4, "Lots of Plants here...etc..", "Garden of Eatin\'");
     // more rooms, different arguments
    }
    So all I have to do is call createRooms(), and all the rooms are created
    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.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    95
    just out of curiosity why didn't you use a constructor to do this?

  8. #8
    Registered User GrNxxDaY's Avatar
    Join Date
    Jul 2002
    Posts
    140
    cause I want all the rooms to have different properties
    i tried using the constructor but i would have had to made twice as many room objects as I actually would use
    Code:
    Room temp1(arguments);
    roomArray[3][5] = temp1;
    
    Room temp2(other arguments);
    roomArray[3][6] = temp2;
    don't want to create all those extra temp variables

    also, i tried this, which didn't work
    Code:
    roomArray[3][6](arguments);
    Is there a better way to do this by using the constructor? like..
    Code:
    roomArray[3][5].Room(arguments);  // Room is name of class
    would that work?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 06:07 PM
  2. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  3. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  4. Religious Bull****
    By Witch_King in forum A Brief History of Cprogramming.com
    Replies: 119
    Last Post: 10-23-2001, 07:14 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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