Thread: array of objects

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    94

    Question array of objects

    If I have the following:
    Code:
    
    struct aStruct
    {
      int val;
      int num;
    };
    
    
    class aClass
    {
      private:
         aStruct aObj[MAX];  <---- array of struct objects???
      public :
         void someFnct();
    };
    
    //--------------------------------------
    void aClass::someFnct()
    {
      aClass clsObj[MAX];  <---- array of class objects???
      ...
    }
    //---------------------------------------
    
    would these create arrays of objects???? Im not to sure how to do this.

    Any help would be appreciated. Thanx

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You'd be correct buddy, the first is an array of objects, and the second is indeed an array of classes.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    ...except that arrays of classes can get tricky. Ponder this...


    Code:
    class Foo {
    
    int number;
    
    Foo( int num ) {
    number = num;
    } 
    
    };
    
    
    
    Foo array[???];
    
    
    How would you approach that, given the requirement that Foo's constructor  must  take at least one argument?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    You can initialize the class array object like this. Here are three elements:

    Code:
    Foo array[] = 
    {
       Foo(0),
       Foo(1),
       Foo(2)
    };
    You should always provide a default constructor in the class definition:

    Foo () { num = 0; }

    BTW I hate using Foo. It sounds like sushi.
    Last edited by Troll_King; 10-18-2002 at 01:29 AM.

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Not really adding to much by the way of practicality, but have a look at this...

    Code:
    #include <iostream>
    #include <new>
    
    class Foo {
    public:
    int number;
    Foo(int num) {number = num;}
    
    };
    
    
    int main()
    {
    	const int nNum = 10;
    	const int nVal = 2;
    	
    	//call global operator new[] to alloc
    	//memory without constructor calls
    	Foo* ptrFoo = static_cast<Foo*>
    		(operator new[](nNum*sizeof(Foo)));
    		
    	//call placement new to call constructors
    	//of each object in pre-alloced memory
    	for(int i = 0;i < nNum;i++)
    		new(ptrFoo+i) Foo(nVal*i);
    		
    	//Read value of each object...	
    	for(int i = 0;i < nNum;i++){
    		std::cout << "Foo number " << i+1;
    		std::cout << " has a value of ";
    		std::cout << ptrFoo[i].number << std::endl;
    	}
    	
    	//Must now make call manual call to destructor..
    	//maybe not needed, but to make a point
    	for(int i = 0;i < nNum;i++)
    		ptrFoo[i].~Foo();
    		
    	//call operator delete[] to free memory	
    	operator delete[] (ptrFoo);	
       
    }
    Scott Meyer's books do some really decent coverage on the new operator (the normal new call), operator new (how the memory is provided) and placement new (how constructors in arrays are called).......this shows how you can take a normal new call apart and access the construction of each individual object....

    BTW...Meyer's books are awsome and well worth a read.

    <edit>Oh...and btw....this compiles ok on Codewarrior and Mingw...but dies a death on VC++6 </edit>

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    That's awesome! I didn't realize you could do that! I will certainly check out that author. That is a really great example. Not sure why VC++ dies, though. Must be a bad object code generation bug in the compiler...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by Sebastiani
    That's awesome! I didn't realize you could do that! I will certainly check out that author. That is a really great example. Not sure why VC++ dies, though. Must be a bad object code generation bug in the compiler...
    VC++6's standard complience is pretty poor.....there's lots of things that will compile on most c++ compilers that will choke on VC++6 (most especially STL stuff)

    VC++.NET might be a different story...but as I am uninterested in paying for a new compiler right now I am not sure

  8. #8
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    I don't get the distinction between an array of objects & an array of classes. Both aStruct aObj[MAX] and aClass clsObj[MAX] are arrays of objects, one of type sStruct and the other of type aClass.

    In C++, the only difference between struct and class is that in struct members are public by default, while in class they're private by default.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  3. Replies: 4
    Last Post: 10-16-2003, 11:26 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Adding objects to an array
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-27-2001, 09:24 AM