TITLE: Another variation on initializing object arrays


PROBLEM: rcs06354@zach.fit.edu (S. Chalavadi)

Can anyone help me in this

class Y {
  int j;
  X tempx[100]; // array of classs X
public :
  Y(){}
  Y(int t) { j=t;}
};


Now how can i initialize array tempx ,with  a  specific constructor 
 X(char *, int),  in class Y (again tempx is array of class
X) .Can any one suggest me how i should write a constructor in the container
class Y that takes care of this initialization.


RESPONSE: fjh@munta.cs.mu.OZ.AU (Fergus James HENDERSON), 16 Jun 93

You can use placement operator new and explicit calls to delete to do this.
Eg.
	#include <new.h>

	Y::Y() {
		// at this stage, tempx will have been initialized
		// using the default constructor. If we want to
		// reinitialize it, we need to destroy the old values first:

		for (int i = 0; i < 100; i++) {
			tempx[i].~X();
		}

		// now initialize using different constructor
		for (i = 0; i < 100; i++) {
			new(tempx[i]) X("argument to constructor", 1);
		}
	}
