473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

calling constructor when allocating an array

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
Jul 19 '05 #1
21 17820
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>

Jul 19 '05 #2
Philipp wrote:
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[i]->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?

--
Attila aka WW
Jul 19 '05 #3
Philipp wrote:
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[i]->MyClass(i);


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.

Jul 19 '05 #4
Attila Feher wrote:
int N = 22;
pointerArray* MyClass;


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.

Jul 19 '05 #5

"Philipp" <_N******************@hotmail.com> wrote in message
news:3f********@epflnews.epfl.ch...
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>


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

Jul 19 '05 #6

"Philipp" <_N******************@hotmail.com> wrote in message news:3f********@epflnews.epfl.ch...
int N = 22;
pointerArray* MyClass;
pointerArray = new MyClass[N];
for (int i=0; i< N; i++)
pointerArray[i]->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[i].Init(i);

Now you don't even have to worry about deleting the array.
Jul 19 '05 #7

"Howard" <al*****@hotmail.com> wrote in message
news:bk********@dispatch.concentric.net...

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
Jul 19 '05 #8
On Wed, 17 Sep 2003 16:02:05 +0200
"Philipp" <_N******************@hotmail.com> wrote:
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>


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
Jul 19 '05 #9
OK that helped a lot. Thank you (I'm still a bit confused about arrays and
pointers... hmmm, newbie perhaps? :-)
Jul 19 '05 #10

"Attila Feher" <at**********@lmf.ericsson.se> wrote in message news:bk**********@newstree.wise.edt.ericsson.se...
* MyClass;

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.
Jul 19 '05 #11
Ron Natalie wrote:
"Attila Feher" <at**********@lmf.ericsson.se> wrote in message
news:bk**********@newstree.wise.edt.ericsson.se... * MyClass;

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.


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
Jul 19 '05 #12
Buster Copley wrote:
Attila Feher wrote:
int N = 22;
pointerArray* MyClass;
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;


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.

--
WW aka Attila
Jul 19 '05 #13

"White Wolf" <wo***@freemail.hu> wrote in message news:bk**********@phys-news1.kolumbus.fi...
Ron Natalie wrote:
"Attila Feher" <at**********@lmf.ericsson.se> wrote in message
news:bk**********@newstree.wise.edt.ericsson.se... * MyClass;

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.


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.

Jul 19 '05 #14
> > 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.
Jul 19 '05 #15
Ron Natalie wrote:
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.


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.


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
Jul 19 '05 #16
Buster wrote:
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.

--
WW aka Attila
Jul 19 '05 #17
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.
Jul 19 '05 #18
Buster wrote:
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.


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
Jul 19 '05 #19

"White Wolf" <wo***@freemail.hu> wrote in message news:bk**********@phys-news1.kolumbus.fi...
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.


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.
Jul 19 '05 #20
> 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);


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
Jul 19 '05 #21
Ron Natalie wrote:
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.
Yes, I missed the mistake in the code.
You are destined for trouble if you think pointers and arrays are
synonymous.
They are not. Pointers point to single objects.
I do not think that and I have never said I did.
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.
Look at the sig Ron. I *am* Attila. With an uppercase A, and pronounced
Atilla. With an uppercase A/ ;-)
This is not true.
Correction: this may not be true.
There's no rquirements that a non-char pointer even be
able to address bytes.
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.
If people would stop assumingt the entire world is a
freaking
Pentium they'd understand the language a little better.


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

--
WW aka Attila
Jul 19 '05 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Sylvain | last post by:
Let's say I have the following code where a class 'pipo' has 8 instances of foo: class foo { foo ( const char * _name): name = _name { }
26
by: Peter Olcott | last post by:
// // Is there something wrong with my syntax for the // Copy Constructor of an Array Element, or does // the C++ language not support this? // #include <stdio.h> #include <stdlib.h> ...
4
by: Michael | last post by:
Hello, I want to use an object (LowCut) within another object (SampleRateConverter) like it is written as follows: class SampleRateConverter { public: SampleRateConverter( int...
5
by: cpluszen | last post by:
Hi, I have developped a c++ class and I have used it in different programs without problems. Now, I'm modifying a c++ file developped by another person (it is a code example about how using a...
6
by: Francois Grieu | last post by:
Hello, I'm asking myself all kind of questions on allocating an array of struct with proper alignment. Is the following code oorrect ? I'm most interested by the statement t =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.