Declare array of objects with constructor having arguments

B

b83503104

Previously, when my constructor had no arguments, I used this to
declare my objects:
MyClass myObject[3];

Now, my constructor has arguments (MyClass::MyClass(int someVariable)),
how do I declare my objects?
I tried

MyClass myObject[3](100);
and

MyClass myObject(100)[3];

Neither of them work.
I also want to avoid using new (if that is the solution), because (?!)
then I have to write a destructor (?!).

Thanks in advance
 
A

Alf P. Steinbach

* (e-mail address removed):
Previously, when my constructor had no arguments, I used this to
declare my objects:
MyClass myObject[3];

Now, my constructor has arguments (MyClass::MyClass(int someVariable)),
how do I declare my objects?
I tried

MyClass myObject[3](100);
and

MyClass myObject(100)[3];

Neither of them work.

MyClass objects[] = {100, 200, 300};

I also want to avoid using new (if that is the solution), because (?!)
then I have to write a destructor (?!).

No, you don't have to, unless there are external resources to free. The
compiler supplies a destructor for you. And that destructor is usually
enough.
 
Z

Zorro

I have not recently used C++ to that level of detail. So your insight
is appreciated.

In the past, I found no mechanism (elegant or otherwise) to do the same
when a constructor takes several arguments, especially of user-defined
types. For that reason, I extended C++ to do something like this:

MyClass objects[n](arg1, arg2);

As I remember, this was not possible in C++ in any form (as
initialization in the course of a construction, as you are showing in
this post).

Naturally, I am more interested in knowing that it cannot be done in
C++. But I really do not know that for sure.

Regards,
Z.
 
A

Alf P. Steinbach

* Zorro:
I have not recently used C++ to that level of detail. So your insight
is appreciated.

In the past, I found no mechanism (elegant or otherwise) to do the same
when a constructor takes several arguments, especially of user-defined
types. For that reason, I extended C++ to do something like this:

MyClass objects[n](arg1, arg2);

Really? You found it so hard to figure out the syntactical rules of C++
that instead you did the much easier thing, delving into the source code of
your nearest compiler and _extending_ the language?

As I remember, this was not possible in C++ in any form (as
initialization in the course of a construction, as you are showing in
this post).

Exactly what is it you want to achieve? A variable 'n'? Initialization
with arg1 and arg2 of every element?

Then use a std::vector.

Naturally, I am more interested in knowing that it cannot be done in
C++. But I really do not know that for sure.

Hm.
 
Z

Zorro

Sorry about the delay, I had to participate in bible study.

I was hoping to get a yes here is how from someone, or no response at
all. I must politely reply to you, however. I am not sure how else I
could have posted the question. The 'n' was not intentional, but now
that you mentioned it, yes, I have extended C++ in that direction as
well (these are dynamic arrays as apart from using new, and "delete
[]", more like ADA's).

The only source for compiler that I dealt with was the experimental
compiler between the years of 1988 and 1990. It was not possible to get
this (an a lot more) into that hack. For that reason I set out to do my
own, which took more than 6 years.

I cannot say that I do not know about STL, but I have never used it.
Since 1991 I have used my own template library that I did for teaching
a course in C++ (using the experimental compiler).

I got too old to chase journals for techniques, so I thought you may
have something for this case. Or, if I got no answer, I would know I
have something not yet easily possible in C++.

I tried to state the question as well as I could. The issue was
initializing an array using a non-trivial constructor. You indicated a
way of doing it for integers. Actually, I did not know that could be
done when the cells of array were classes (and I have not tried it
yet). Evidently the compiler is calling the constructor on each cell
using the sequence of numbers. Well then, may be there is a way to do
it for more complicated constructors.

The extension I am speaking of is not extending a particular source for
a compiler. I dropped that in 1990. I was speaking of extensions to the
language C++.

Thanks for your time.

Regards,
Dr. Z.
Chief Scientist
(e-mail address removed)
http://www.zhmicro.com
http://distributed-software.blogspot.com
 
B

b83503104

Thanks a lot. Since I cannot find this topic in my book, I have a
question on how to extend this to a constructor with 2 variables like
this:

MyClass::MyClass(int Var1, int Var2);

Is it like this?

MyClass objects[ ] = {{100, 200}, {300, 400}, {500, 600}};

Thanks again.
 
A

Alf P. Steinbach

* (e-mail address removed):
Thanks a lot. Since I cannot find this topic in my book,

Which book is that?

I have a
question on how to extend this to a constructor with 2 variables like
this:

MyClass::MyClass(int Var1, int Var2);

Is it like this?

MyClass objects[ ] = {{100, 200}, {300, 400}, {500, 600}};

No. You can use that syntax for POD types, essentially types you could
have defined in C if C had the same repertoire of basic types as C++. But
when you have a constructor (non-POD) you'll have to do something like

MyClass objects[] = {
MyClass( 100, 200 ), MyClass( 300, 400 ), MyClass( 500, 600 )
};

Using raw arrays there's no way to specify a repeat of a given value n
times, but you can do that using a std::vector:

std::vector<MyClass> objects( n, MyClass( 123, 456 ) );

Using a std::vector there is, on the other hand, no way to specify a list of
specific values like with a raw array, so one solution when that is required
and you want to use a std::vector is to specify the list of initial values
as a raw array constant, and use that to initialize the std::vector. The
Boost library has at least one other solution, as I understand it based on
dynamically constructing a list of values. It just gives a shorter spec.
 
J

Jakob Bieling

* (e-mail address removed):

No, you don't have to, unless there are external resources to free.
The
compiler supplies a destructor for you. And that destructor is
usually
enough.


I would like to add, that having to write a dtor does not have
anything to do with using new or new[]. If you have external resources
to free, you need a dtor in either case.

regards
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top