[comp.lang.c++] `new'ing an object that has a constructor

ecsv38@castle.ed.ac.uk (S Manoharan) (07/12/90)

Well, the subject line says it all. How do I `new' an object
that's got a constructor? I am interested in `new'ing an array
of such objects.

Consider the following code:

class A {
private:
   int sz;
public:
   A(int n)     { sz = n; }
   void foo()   { cout << form("%d\n", sz); }
};

int main()
{
   A *p1 = new A(3);
   A *p2 = new A[10]; // <--------- Problem's here.

   p1->foo();
   return 0;
}

I am using g++ 1.37.1. It complains "too few arguments for 
constructor `A'". Fine. Now if I change the line to:
   A *p2 = new A(3)[10]; // <--------- Problem's here.
I get a complaint "parse error before `['".

Where do I go wrong? Or, how do I get around this?

Lotsa thanks in advance.

Manoharan.

Ps - I tried it with cfront 1.2.1 [Sorry, that's all we've got
here] and it gives a compile time Memory fault in both cases. 
[ 701 Memory fault in the 1st case and 695 Mem Fault in the 2nd ]

jfischer@sco.COM (Jonathan Fischer) (07/13/90)

In article <5137@castle.ed.ac.uk> ecsv38@castle.ed.ac.uk (S Manoharan) writes:
>Well, the subject line says it all. How do I `new' an object
>that's got a constructor? I am interested in `new'ing an array
>of such objects.

	Unfortunately, you can't 'new' an array of objects unless the
class has a default constructor.  This means that you can't pass an
initializer to a new stmt for an array.  The default constructor is called
for each element of the array (in ascending order).

	So, your "A *p2 = new A[10];" would work if you added a default
constructor to A.  Of course, you'd then have to set up each element,
and to do that you'd need some sort of A::SetValues() member function.
Not too nice.

	To initialize the elements in the declaration, rather than

>   A *p2 = new A(3)[10];

you can try:

    A p2[10] = { 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 };

	Of course this isn't dynamic.  If you want a dynamically-allocated
array of A and you want to pass a value to the constructor, do the following:

    A **p = new A* [ ArraySize ];
    for ( int i = 0; i < ArraySize; ++i ) {
      p[i] = new A( InitialValue );
    }

Here each p[i] is a pointer to A, where each p2[i] above is an actual
object A.
-- 
Jonathan Fischer	     SCO Canada, Inc.	    Toronto, Ontario, Canada

Usenet's first law of Flamodynamics:
For every opinion, there is an equal and opposite counter-opinion.

jimad@microsoft.UUCP (Jim ADCOCK) (07/18/90)

In article <5137@castle.ed.ac.uk> ecsv38@castle.ed.ac.uk (S Manoharan) writes:
>Well, the subject line says it all. How do I `new' an object
>that's got a constructor? I am interested in `new'ing an array
>of such objects.

Here's an example of a work-around for arrays.  Use a static member function
to provide a default for initializing objects.  This could be generalized
to providing a default function for initializing object ....

extern "C"
{
#include "stdio.h"
}

class VAL
{
	int i;
	static int valdefault;
public:
	static void SetDefault(int d) { valdefault = d; }
	VAL() : i(valdefault) { }
	VAL(int ii) : i(ii) { }
	void SetVal(int ii) { i = ii; }
	int GetVal() { return i; }
	void Print() { printf("%d\n",i); }
};
int VAL::valdefault = 0;

main()
{
	int i;

	VAL::SetDefault(234);
	VAL* AVAL = new VAL[5];

	for (i=0; i<5; ++i)
		AVAL[i].Print();
	putchar('\n');

	VAL::SetDefault(432);
	VAL* AVAL2 = new VAL[5];

	for (i=0; i<5; ++i)
		AVAL2[i].Print();
	putchar('\n');

	return 0;
}