calling constructor when allocating an array

P

Philipp

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->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
 
P

Philipp

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->MyClass(i);

</code>
 
A

Attila Feher

Philipp said:
Sorry my code was wrong... Is this correct?

<code>

int N = 22;
pointerArray* MyClass;

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

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


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?
 
B

Buster Copley

Philipp said:
int N = 22;
pointerArray* MyClass;

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

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


Should be
for (int i = 0; i < N; ++ i) pointerArray = MyClass (i);
or
for (int i = 0; i < N; ++ i)
{
pointerArray .~MyClass ();
new (& pointerArray ) 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 .~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.
 
B

Buster Copley

Attila said:
This is not a pointer array. It is a pointer to an array.

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.
 
R

Ron Natalie

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


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.Init(i);

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

Howard

Philipp said:
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->MyClass(i);

</code>


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 = new MyClass(i); // create each instance!

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

for (int i = (N-1); i >= 0; --i)
delete pointerArray; // 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
 
H

Howard

Howard said:
pointerArray* MyClass[N]; // no need to "new" this!


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

MyClass* pointerArray[22];

-Howard
 
C

Clemens Auer

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->MyClass(i);

</code>


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 = new MyClass(i);


regards
Clemens
 
P

Philipp

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

White Wolf

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

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. ;-)
 
R

Ron Natalie

White Wolf said:
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. ;-)
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.
 
W

White Wolf

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

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

This is playing with the words.

MyClass *pointerArray;

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

Buster

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

This is playing with the words.

MyClass *pointerArray;

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

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

White Wolf

Ron said:
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.

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. :)
 
W

White Wolf

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

This is playing with the words.

MyClass *pointerArray;

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

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

The start of the array is the first element.
 
B

Buster

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. :)

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.
 
R

Ron Natalie

White Wolf said:
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 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.
 
W

White Wolf

Buster said:
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.

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. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top