Connecting Tech Pros Worldwide Forums | Help | Site Map

calling constructor when allocating an array

Philipp
Guest
 
Posts: n/a
#1: Jul 19 '05
Hello, a very simple question:
Ok I have a class MyClass with a constructor MyClass(int) (no constructor
without argument defined)

how can I make an array of pointers to objects of that class, calling the
constructor with the index number as argument?

<code>
int N = 22;
pointerArray = new MyClass*[N];
for (int i=0; i< N; i++)
pointerArray[i]->MyClass(i);
</code>

is this correct? does the second line call some default constructor for
MyClass? Any better idea how to do that?

Thanks Phil



Philipp
Guest
 
Posts: n/a
#2: Jul 19 '05

re: calling constructor when allocating an array


Sorry my code was wrong... Is this correct?

<code>

int N = 22;
pointerArray* MyClass;
pointerArray = new MyClass[N];
for (int i=0; i< N; i++)
pointerArray[i]->MyClass(i);

</code>



Attila Feher
Guest
 
Posts: n/a
#3: Jul 19 '05

re: calling constructor when allocating an array


Philipp wrote:[color=blue]
> Sorry my code was wrong... Is this correct?
>
> <code>
>
> int N = 22;
> pointerArray* MyClass;[/color]

This is not a pointer array. It is a pointer to an array.
[color=blue]
> pointerArray = new MyClass[N];[/color]

new creates an array of N pieces of objects of MyClass type.
[color=blue]
> for (int i=0; i< N; i++)
> pointerArray[i]->MyClass(i);[/color]

This is not good. The MyClass types are already constructed. And you
cannot call constructors, they have no name. What do you want to do?

--
Attila aka WW


Buster Copley
Guest
 
Posts: n/a
#4: Jul 19 '05

re: calling constructor when allocating an array


Philipp wrote:
[color=blue]
> int N = 22;
> pointerArray* MyClass;[/color]

You mean 'MyClass * pointerArray;'. Why not post the real code?
[color=blue]
> pointerArray = new MyClass[N];[/color]

This calls the default constructor for MyClass N times.
[color=blue]
> for (int i=0; i< N; i++)
> pointerArray[i]->MyClass(i);[/color]

Should be
for (int i = 0; i < N; ++ i) pointerArray [i] = MyClass (i);
or
for (int i = 0; i < N; ++ i)
{
pointerArray [i].~MyClass ();
new (& pointerArray [i]) MyClass (i);
}

If you allocate raw memory instead of objects:
pointerArray = reinterpret_cast <MyClass *>
(new char [N * sizeof (MyClass)]);

then you don't need the destructor call before the
placement new inside the for loop. However, in that
case you would need:

for (int i = 0; i < N; ++ i) pointerArray [i].~MyClass ();
delete [] reinterpret_cast <char *> (pointerArray);

instead of just 'delete [] pointerArray;'.

Basically, the point is that you can't call a constructor on an
object (because by the time it is an object, it has already been
constructed).

I hope this helps, and that I haven't made too many mistakes of my own.
Regards,
Buster.

Buster Copley
Guest
 
Posts: n/a
#5: Jul 19 '05

re: calling constructor when allocating an array


Attila Feher wrote:[color=blue][color=green]
>>int N = 22;
>>pointerArray* MyClass;[/color]
>
> This is not a pointer array. It is a pointer to an array.[/color]

No, it's a syntax error. A pointer to an object would look like this:
MyClass * pointerArray;

A pointer to an array would look like this:
MyClass (* pointerArray) [NN];
// NN is a (compile-time) integral constant

Regards,
Buster.

Howard
Guest
 
Posts: n/a
#6: Jul 19 '05

re: calling constructor when allocating an array



"Philipp" <_NO_S~P~A~M_kitschen@hotmail.com> wrote in message
news:3f686951$1@epflnews.epfl.ch...[color=blue]
> Sorry my code was wrong... Is this correct?
>
> <code>
>
> int N = 22;
> pointerArray* MyClass;
> pointerArray = new MyClass[N];
> for (int i=0; i< N; i++)
> pointerArray[i]->MyClass(i);
>
> </code>[/color]

No. It would be much easier if you had a default constructor. Then you
could just declare an array of objects instead of pointers. But to create
an array of pointers, you first need to declare the array, then create
instances of the objects for each array item to point to. You can't just
call a constructor like a function...you have to "new" each pointer, like
this:

int N = 22;
pointerArray* MyClass[N]; // no need to "new" this!
for (int i = 0; i < N; ++i)
pointerArray[i] = new MyClass(i); // create each instance!

....and, later, to delete...

for (int i = (N-1); i >= 0; --i)
delete pointerArray[i]; // delete each instance

(BTW, you could count upwards in the delete loop, I just got in the practice
long ago of deleting in the opposite order I allocated in, because on some
systems it prevented memory fragmentation...but that's just me.)

-Howard





Ron Natalie
Guest
 
Posts: n/a
#7: Jul 19 '05

re: calling constructor when allocating an array



"Philipp" <_NO_S~P~A~M_kitschen@hotmail.com> wrote in message news:3f686951$1@epflnews.epfl.ch...
[color=blue]
> int N = 22;
> pointerArray* MyClass;
> pointerArray = new MyClass[N];
> for (int i=0; i< N; i++)
> pointerArray[i]->MyClass(i);[/color]

You can NOT call constructors at all. They are called for you as part
of normal object creation. You can't create an array with other that
default initialization. A vector, which is probably better suited for what
you want to do anyhow, can be initialized with a non-default object, but
it is the same for all elements.

Besides, you're not initializing the array in the C++ sense. Initialization
via the default constructor occurs when the new is invoked. But you
can do what you are trying to do if you just put allt he stuff that would have
been in your MyClass(int) constructor in a regular member function.

Your class would look like:
class MyClass {
public:
MyClass(); // default constructor required
MyClass(int i) { Init(i); }

void Init(int i);
};

vector<MyClass> pointerArray(N);
for(int i = 0; i < N; ++i) pointerArray[i].Init(i);

Now you don't even have to worry about deleting the array.


Howard
Guest
 
Posts: n/a
#8: Jul 19 '05

re: calling constructor when allocating an array



"Howard" <alicebt@hotmail.com> wrote in message
news:bk9r3g$577@dispatch.concentric.net...[color=blue]
>
> pointerArray* MyClass[N]; // no need to "new" this![/color]


DOH! Now he's goe ME doing it! :-) That should be (of course)

MyClass* pointerArray[22];

-Howard


Clemens Auer
Guest
 
Posts: n/a
#9: Jul 19 '05

re: calling constructor when allocating an array


On Wed, 17 Sep 2003 16:02:05 +0200
"Philipp" <_NO_S~P~A~M_kitschen@hotmail.com> wrote:
[color=blue]
> Sorry my code was wrong... Is this correct?
>
> <code>
>
> int N = 22;
> pointerArray* MyClass;
> pointerArray = new MyClass[N];
> for (int i=0; i< N; i++)
> pointerArray[i]->MyClass(i);
>
> </code>[/color]

i think you're trying to do something like this:

int N = 22;
// the ** makes the array of pointer not array of objects
MyClass **pointerArray;

// crerate all the Pointer
pointerArray = new (MyClass*)[N];

// create all the objects
for (int i=0; i< N; i++)
pointerArray[i] = new MyClass(i);


regards
Clemens
Philipp
Guest
 
Posts: n/a
#10: Jul 19 '05

re: calling constructor when allocating an array


OK that helped a lot. Thank you (I'm still a bit confused about arrays and
pointers... hmmm, newbie perhaps? :-)


Ron Natalie
Guest
 
Posts: n/a
#11: Jul 19 '05

re: calling constructor when allocating an array



"Attila Feher" <attila.feher@lmf.ericsson.se> wrote in message news:bk9q88$1o7$1@newstree.wise.edt.ericsson.se...
* MyClass;[color=blue]
>
> This is not a pointer array. It is a pointer to an array.[/color]

Well it's a pointer to the first element of an array.


White Wolf
Guest
 
Posts: n/a
#12: Jul 19 '05

re: calling constructor when allocating an array


Ron Natalie wrote:[color=blue]
> "Attila Feher" <attila.feher@lmf.ericsson.se> wrote in message
> news:bk9q88$1o7$1@newstree.wise.edt.ericsson.se... * MyClass;[color=green]
>>
>> This is not a pointer array. It is a pointer to an array.[/color]
>
> Well it's a pointer to the first element of an array.[/color]

Yes. This is the way it goes when we point to a part of the memory. We
point to the beginning of it. Like a pointer to a double will point to its
first byte. ;-)

--
WW aka Attila


White Wolf
Guest
 
Posts: n/a
#13: Jul 19 '05

re: calling constructor when allocating an array


Buster Copley wrote:[color=blue]
> Attila Feher wrote:[color=green][color=darkred]
>>> int N = 22;
>>> pointerArray* MyClass;[/color]
>>
>> This is not a pointer array. It is a pointer to an array.[/color]
>
> No, it's a syntax error. A pointer to an object would look like this:
> MyClass * pointerArray;[/color]

Yeah. I missed that one. :-)
[color=blue]
> A pointer to an array would look like this:
> MyClass (* pointerArray) [NN];
> // NN is a (compile-time) integral constant[/color]

This is playing with the words.

MyClass *pointerArray;

will point to the array allocated by the new[] operator.

--
WW aka Attila


Ron Natalie
Guest
 
Posts: n/a
#14: Jul 19 '05

re: calling constructor when allocating an array



"White Wolf" <wolof@freemail.hu> wrote in message news:bka12c$gu2$1@phys-news1.kolumbus.fi...[color=blue]
> Ron Natalie wrote:[color=green]
> > "Attila Feher" <attila.feher@lmf.ericsson.se> wrote in message
> > news:bk9q88$1o7$1@newstree.wise.edt.ericsson.se... * MyClass;[color=darkred]
> >>
> >> This is not a pointer array. It is a pointer to an array.[/color]
> >
> > Well it's a pointer to the first element of an array.[/color]
>
> Yes. This is the way it goes when we point to a part of the memory. We
> point to the beginning of it. Like a pointer to a double will point to its
> first byte. ;-)
>[/color]
No, pointers point to complete objects as far as the language is concerend.
MyClass* points to one MyClass instance which happens to be the first
element of the array.



Buster
Guest
 
Posts: n/a
#15: Jul 19 '05

re: calling constructor when allocating an array


> > A pointer to an array would look like this:[color=blue][color=green]
> > MyClass (* pointerArray) [NN];
> > // NN is a (compile-time) integral constant[/color]
>
> This is playing with the words.
>
> MyClass *pointerArray;
>
> will point to the array allocated by the new[] operator.[/color]

Sorry, no. To the first element. I know what you mean,
but it isn't what you said.


White Wolf
Guest
 
Posts: n/a
#16: Jul 19 '05

re: calling constructor when allocating an array


Ron Natalie wrote:[color=blue][color=green][color=darkred]
>>>> This is not a pointer array. It is a pointer to an array.
>>>
>>> Well it's a pointer to the first element of an array.[/color]
>>
>> Yes. This is the way it goes when we point to a part of the memory.
>> We
>> point to the beginning of it. Like a pointer to a double will point
>> to its
>> first byte. ;-)
>>[/color]
> No, pointers point to complete objects as far as the language is
> concerend.
> MyClass* points to one MyClass instance which happens to be the first
> element of the array.[/color]

SET PEDANTIC=OFF

In any case: a pointer array (as far as I know English) is an array of
pointers. I do not feel my description too misleading. It might not be
pedantic to call it a pointer to an array but it does point to an array of
MyClass instances.

I understand that (strictly speaking) if I say pointer to an array one might
say: OK, so if I then say ++ptr, then it will point to the next array. And
of course this is not the case. :-)

--
WW aka Attila


White Wolf
Guest
 
Posts: n/a
#17: Jul 19 '05

re: calling constructor when allocating an array


Buster wrote:[color=blue][color=green][color=darkred]
>>> A pointer to an array would look like this:
>>> MyClass (* pointerArray) [NN];
>>> // NN is a (compile-time) integral constant[/color]
>>
>> This is playing with the words.
>>
>> MyClass *pointerArray;
>>
>> will point to the array allocated by the new[] operator.[/color]
>
> Sorry, no. To the first element. I know what you mean,
> but it isn't what you said.[/color]

The start of the array is the first element.

--
WW aka Attila


Buster
Guest
 
Posts: n/a
#18: Jul 19 '05

re: calling constructor when allocating an array


[color=blue]
> In any case: a pointer array (as far as I know English) is an array of
> pointers. I do not feel my description too misleading. It might not be
> pedantic to call it a pointer to an array but it does point to an array of
> MyClass instances.
>
> I understand that (strictly speaking) if I say pointer to an array one might
> say: OK, so if I then say ++ptr, then it will point to the next array. And
> of course this is not the case. :-)[/color]

Confusing = false;

Yup. That's why in the standard it says that the value of an array
new-expression is a pointer to the first element of the array, rather
than saying it's a pointer to the array.

What do you say when you _do_ mean 'pointer to an array'?

In any case, I didn't mean to wind you up. Sorry.

Regards,
Buster.


White Wolf
Guest
 
Posts: n/a
#19: Jul 19 '05

re: calling constructor when allocating an array


Buster wrote:[color=blue][color=green]
>> I understand that (strictly speaking) if I say pointer to an array
>> one might say: OK, so if I then say ++ptr, then it will point to the
>> next array. And of course this is not the case. :-)[/color]
>
> Confusing = false;
>
> Yup. That's why in the standard it says that the value of an array
> new-expression is a pointer to the first element of the array, rather
> than saying it's a pointer to the array.
>
> What do you say when you _do_ mean 'pointer to an array'?
>
> In any case, I didn't mean to wind you up. Sorry.[/color]

I am programming from 1984. I have never needed to say or use a pointer to
an array. So something along the lines of that bracketed nice declaration.
I know how to write it and I never needed to use it. :-)

--
WW aka Attila


Ron Natalie
Guest
 
Posts: n/a
#20: Jul 19 '05

re: calling constructor when allocating an array



"White Wolf" <wolof@freemail.hu> wrote in message news:bka1v8$kpn$1@phys-news1.kolumbus.fi...
[color=blue][color=green]
> > No, pointers point to complete objects as far as the language is
> > concerend.
> > MyClass* points to one MyClass instance which happens to be the first
> > element of the array.[/color]
>
> In any case: a pointer array (as far as I know English) is an array of
> pointers. I do not feel my description too misleading. It might not be
> pedantic to call it a pointer to an array but it does point to an array of
> MyClass instances.[/color]

I have no qualms with whether pointerArray is an array of pointer or
pointer to an array. However, it the example given, you HAVE NEITHER.

You are destined for trouble if you think pointers and arrays are synonymous.
They are not. Pointers point to single objects.

However my comments were specifically directed at the comment from attilla
that said that a pointer might be thought of pointing to the first byte. This
is not true. There's no rquirements that a non-char pointer even be able to
address bytes. If people would stop assumingt the entire world is a freaking
Pentium they'd understand the language a little better.


Philipp
Guest
 
Posts: n/a
#21: Jul 19 '05

re: calling constructor when allocating an array


> i think you're trying to do something like this:[color=blue]
>
> int N = 22;
> // the ** makes the array of pointer not array of objects
> MyClass **pointerArray;
>
> // crerate all the Pointer
> pointerArray = new (MyClass*)[N];
>
> // create all the objects
> for (int i=0; i< N; i++)
> pointerArray[i] = new MyClass(i);[/color]

Yes exactly! That's what I wanted to do and after thinking for 5 minutes by
myself I figured it out...
I just was confused that one of the ' * ' is for the array and the second
one is for the actual pointers stored in the array.

Thanks to all of you for your answers.
Phil


White Wolf
Guest
 
Posts: n/a
#22: Jul 19 '05

re: calling constructor when allocating an array


Ron Natalie wrote:[color=blue]
> I have no qualms with whether pointerArray is an array of pointer or
> pointer to an array. However, it the example given, you HAVE
> NEITHER.[/color]

Yes, I missed the mistake in the code.
[color=blue]
> You are destined for trouble if you think pointers and arrays are
> synonymous.
> They are not. Pointers point to single objects.[/color]

I do not think that and I have never said I did.
[color=blue]
> However my comments were specifically directed at the comment from
> attilla
> that said that a pointer might be thought of pointing to the first
> byte.[/color]

Look at the sig Ron. I *am* Attila. With an uppercase A, and pronounced
Atilla. With an uppercase A/ ;-)
[color=blue]
> This is not true.[/color]

Correction: this may not be true.
[color=blue]
> There's no rquirements that a non-char pointer even be
> able to address bytes.[/color]

But it is guaranteed that when converted to char* (or unsigned char* or
signed char*) it will point to the first byte of the storage representing
that object.
[color=blue]
> If people would stop assumingt the entire world is a
> freaking
> Pentium they'd understand the language a little better.[/color]

If people would stop assuming what other people assume we would get less war
and more beer to drink.

--
WW aka Attila


Closed Thread