   Is this possible without a default constructor? C++
class SomeObject{public:SomeObject(int mustHave1, int mustHave2);};class OtherObject{public:OtherObject(int one, int two)protected:SomeObject array[4];};OtherObject::OtherObject(int one, int two):   // need default here, only way to handle this?   array[0](one, two) // <-- yikes? nothing like this can be done? looks silly btw{}
  Maybe you could do that with templates.template <int mustHave1, int mustHave2>class SomeObject{public:    SomeObject();};template <int one, int two>class OtherObject{public:    OtherObject();protected:    SomeObject<one, two> array[4];};;  Interesting, possible maybe, little more rewriting then I wanted but... anyone else? thanks for suggestion.   ;  I think this might work...class SomeObject{public:    SomeObject(int mustHave1, int mustHave2);};class OtherObject{public:    OtherObject(int one, int two);    virtual ~OtherObject();protected:    SomeObject *array;};OtherObject::OtherObject(int one, int two){    array = new SomeObject[4](one, two);}OtherObject::~OtherObject() {    delete [] array;    array = 0;};http://cpptips.hyperformix.com/cpptips/array_init5Nope, it's not possible. (Who knows why). It also doesn't seem possible with boost::array, so if you are dead set on not having a default constructor (ignoring the template hack above), you're going to have to use something like std::vector.Edit: I was assuming the same initializer for the whole array was unacceptable as in the second suggestion.   ;  The simplest solution is to just use a std::vector or equivalent.  If you really need the array to be within the object and not dynamically allocated then you can get close, although it gets ugly fast.  This technique is based on Andrei Alexandrescu's Discriminated Unions articles in his Generic<Programming> column (google it).  I make no guarantees that there aren't better ways.  This should likely only be used as a last resort if all other techniques are unsuitable.The best explanation can probably be gained by reading the articles mentioned above, but briefly:We need to arrange to have an array of memory guaranteed to be large enough to store 4 SomeObjects in and guaranteed to have the correct alignment.  The correct alignment can be computed using Andrei's beautiful template trickery, of which my own implementation (with some renaming) is below.  This gives us an alignment which is not too small (which could be disasterous) but is not too large (which could be inefficient).  You could skip this complicated step and just use the largest primitive type and accept a potential performance penalty.  Or you could use a hand-selected "best type" and accept that any changes to SomeClass could break the array.Once we have a suitable buffer of memory we need to be able to construct objects into it and destroy them.  This can be done by calling placement new and manually calling the destructors.  Note that the destructors must be called manually.  Note also that unless we construct an object in an array element no object exists at that location and also that constructing more than one object at the same location is an error.  Each element of the array must be constructed exactly once and destructed exactly once (or else never constructed and never destructed or constructed once, then undergo any number of destruction-reconstruction pairs and then be destructed once).  You will also need to implement a copy-constructor and copy-assignment operator.  I also made OtherClass's protected data private, since there is no way you want a derived class mucking around with it.  Provide protected member functions if derived classes may need to modify the array directly.I repeat: This should likely only be used as a last resort if all other techniques are unsuitable.  Depending on use it can be very error prone.  It is safest if all elements of the array are always initialised in every constructor.#include <iostream>#include <new>#include <utility>template < bool Condition, typename TrueResult, typename FalseResult >struct Select;template < typename TrueResult, typename FalseResult >struct Select< true, TrueResult, FalseResult >{public:typedef TrueResult Result;};template < typename TrueResult, typename FalseResult >struct Select< false, TrueResult, FalseResult >{public:typedef FalseResult Result;};struct TypeListEnd{};template < typename Head, typename Tail >struct TypeList{};struct Unknown;template < typename Type >struct Structify{Type type;};typedef TypeList< char,TypeList< short int,TypeList< int,TypeList< long int,TypeList< float,TypeList< double,TypeList< long double,TypeList< char*,TypeList< short int*,TypeList< int*,TypeList< long int*,TypeList< float*,TypeList< double*,TypeList< long double*,TypeList< void*,TypeList< Unknown (*)(Unknown),TypeList< Unknown* Unknown::*,TypeList< Unknown (Unknown::*)(Unknown),TypeList< Structify< char >,TypeList< Structify< short int >,TypeList< Structify< int >,TypeList< Structify< long int >,TypeList< Structify< float >,TypeList< Structify< double >,TypeList< Structify< long double >,TypeList< Structify< char* >,TypeList< Structify< short int* >,TypeList< Structify< int* >,TypeList< Structify< long int* >,TypeList< Structify< float* >,TypeList< Structify< double* >,TypeList< Structify< long double* >,TypeList< Structify< void* >,TypeList< Structify< Unknown (*)(Unknown) >,TypeList< Structify< Unknown* Unknown::* >,TypeList< Structify< Unknown (Unknown::*)(Unknown) >, TypeListEnd > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > AllAlignableTypes;template < typename TypeList, std::size_t targetSize >struct RemoveTypesWithWorseAlignmentRestrictions;template < std::size_t targetSize >struct RemoveTypesWithWorseAlignmentRestrictions< TypeListEnd, targetSize >{public:typedef TypeListEnd Result;};template < typename Head, typename Tail, std::size_t targetSize >struct RemoveTypesWithWorseAlignmentRestrictions< TypeList< Head, Tail >, targetSize >{public:typedef typename RemoveTypesWithWorseAlignmentRestrictions< Tail, targetSize >::Result MinimalTail;typedef typename Select< sizeof(Head) <= targetSize, TypeList< Head, MinimalTail >, MinimalTail >::Result Result;};template < typename TypeList >union ConfigurableUnion;template < typename Head >union ConfigurableUnion< TypeList< Head, TypeListEnd > >{Head head;};template < typename Head, typename Tail >union ConfigurableUnion< TypeList< Head, Tail > >{Head head;ConfigurableUnion< Tail > tail;};template < typename Type >struct WorstAlign{private:typedef typename RemoveTypesWithWorseAlignmentRestrictions< AllAlignableTypes, sizeof(Type) >::Result MinimumAlignmentTypes;public:typedef ConfigurableUnion< MinimumAlignmentTypes > WorstAlignmentType;};class SomeObject{public:SomeObject(int mustHave1, int mustHave2){std::cout << "Constructed @ " << this << " with " << mustHave1 << ", " << mustHave2 << '\n';}~SomeObject(){std::cout << "Destructed @ " << this << '\n';}};class OtherObject{public:OtherObject(int one, int two, int three, int four, int five, int six);OtherObject(OtherObject const & oo);~OtherObject();OtherObject & operator=(OtherObject const & oo);private:union{char raw[sizeof(SomeObject) * 4];WorstAlign< SomeObject >::WorstAlignmentType align;} rawArray;SomeObject * const array;};OtherObject::OtherObject(int one, int two, int three, int four, int five, int six):array(reinterpret_cast< SomeObject * >(&rawArray.raw[0])){std::cout << "Raw array @ " << static_cast< void * >(&rawArray.raw[0]) << '\n';new (&array[0]) SomeObject(one, two);new (&array[1]) SomeObject(three, four);new (&array[2]) SomeObject(five, six);// note that array[3] does not exist and must not be destructed}OtherObject::OtherObject(OtherObject const & oo):array(reinterpret_cast< SomeObject * >(&rawArray.raw[0])){std::cout << "Raw array @ " << static_cast< void * >(&rawArray.raw[0]) << '\n';new (&array[0]) SomeObject(oo.array[0]);new (&array[1]) SomeObject(oo.array[1]);new (&array[2]) SomeObject(oo.array[2]);}OtherObject::~OtherObject(){array[0].~SomeObject();array[1].~SomeObject();array[2].~SomeObject();}OtherObject & OtherObject::operator=(OtherObject const & oo){// array[0..2] MUST have been constructed previously for both objectsarray[0] = oo.array[0];array[1] = oo.array[1];array[2] = oo.array[2];return *this;}int main(){OtherObject oo(1, 2, 3, 4, 5, 6);}Enigma   ;  Thanks for the replys. The reason I ask is because the "4" that I used in the example would be a constant (an enum) used to index that array, and will only change if necessary later on if the enum was to grow (but I would be recompiling to change the constant) so I just wanted a static array size, not wanting to allocate memory in this case. "mustHave1" and "mustHave2" are used to allocate memory inside "SomeObject" and not having a default constructor ensures (atleast it has been up until now) that the right amount is allocated once, and no reallocation is needed later (possibly 4 or 5 lines later in that same constructor of "OtherObject".I think Ive got some ideas now, atleast I know its not easily done so ill prep for this situation later. Thanks.   