By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,925 Members | 1,134 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,925 IT Pros & Developers. It's quick & easy.

Declare array of objects with constructor having arguments

P: n/a
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

Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
* b8*******@yahoo.com:
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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2

P: n/a
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.

Jul 23 '05 #3

P: n/a
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #4

P: n/a
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
zo****@ZHMicro.com
http://www.zhmicro.com
http://distributed-software.blogspot.com

Jul 23 '05 #5

P: n/a
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.

Jul 23 '05 #6

P: n/a
* b8*******@yahoo.com:
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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #7

P: n/a
"Alf P. Steinbach" <al***@start.no> wrote in message
news:42***************@news.individual.net...
* b8*******@yahoo.com:

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.

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

(reply address in rot13, unscramble first)
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.