Thread: Quick Class Question (say that 3 times)

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

    Quick Class Question (say that 3 times)

    if i do something like this...

    Code:
    class People
    {
     // all my members & such
    }arrayOfPeople[960];
    will it allocate (is this the right term?) memory for those 960 elements? or just the ones i initialize?

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    421
    well, that code instantiates them. and if they are instantiated they are initialised/constructed.

    U.
    Quidquid latine dictum sit, altum sonatur.
    Whatever is said in Latin sounds profound.

  3. #3
    Unregistered
    Guest
    I think space for 960 instances of the class People will be assigned/allocated but I don't believe any instances of People will be initialized/constructed. If they are initialized it would be with the default constructor. Therefore, if class People contained a public data member called age, and if the default constructor assigned age a value of 0, and if each element of the array was an actual instance of class People, then you should be able to do this:

    cout << arrayOfPeople[99].age;

    and the value of 0 would appear on the screen. I don't have compiler to prove it, but I feel strongly that if you were to do so the compiler will indicate error on your attempt and prevent anything from appearing in the console or it will allow compilation but at runtime you will get garbage or unpredictable results appearing.

    If I'm right it's sort of like building a 4 passenger car but not putting any people in it. The space is there, it's been allocated, but there is nothing in the space unless you put it there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. class question
    By l2u in forum C++ Programming
    Replies: 5
    Last Post: 12-02-2006, 04:55 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. how can I re-sort a map
    By indigo0086 in forum C++ Programming
    Replies: 8
    Last Post: 06-01-2006, 06:21 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM