Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Declare array of objects with constructor having arguments

Reply
Thread Tools

Declare array of objects with constructor having arguments

 
 
b83503104@yahoo.com
Guest
Posts: n/a
 
      07-20-2005
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

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-20-2005
* http://www.velocityreviews.com/forums/(E-Mail 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.

--
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?
 
Reply With Quote
 
 
 
 
Zorro
Guest
Posts: n/a
 
      07-20-2005
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.

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-20-2005
* 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?
 
Reply With Quote
 
Zorro
Guest
Posts: n/a
 
      07-21-2005
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 Removed)
http://www.zhmicro.com
http://distributed-software.blogspot.com

 
Reply With Quote
 
b83503104@yahoo.com
Guest
Posts: n/a
 
      07-21-2005
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.

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-21-2005
* (E-Mail 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.

--
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?
 
Reply With Quote
 
Jakob Bieling
Guest
Posts: n/a
 
      07-21-2005
"Alf P. Steinbach" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

>* (E-Mail Removed):


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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
declare/initialize array in constructor? BillJosephson Java 5 12-14-2006 02:46 PM
how to declare an array of objects without "new" b83503104@yahoo.com C++ 16 07-11-2005 04:14 PM
Temporary objects as constructor arguments? Andy Buckley C++ 5 08-26-2004 01:32 AM
declare array of objects in header file Philipp C++ 5 10-16-2003 06:08 PM



Advertisments