473,385 Members | 1,867 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,385 software developers and data experts.

Declare array of objects with constructor having arguments

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
7 11539
* 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
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
* 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
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
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
* 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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: b83503104 | last post by:
Hi, class MyClass{ int array_size; HisClass **hisObject; }; I want hisObject to point to an array of HisClass objects. The size of the array is given by array_size.
15
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have ...
19
by: Tom Jastrzebski | last post by:
Hello, I was just testing VB.Net on Framework.Net 2.0 performance when I run into the this problem. This trivial code attached below executed hundreds, if not thousand times faster in VB 6.0...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
by: Sam Kong | last post by:
Hello! My question would not be very practical but just out of curiosity. In JavaScript, Array is a subclass of Object. Well maybe not exactly... but sort of... For normal objects, you can...
2
by: JJA | last post by:
I'm looking at some code I do not understand: var icons = new Array(); icons = new GIcon(); icons.image = "somefilename.png"; I read this as an array of icons is being built. An element of...
2
by: Howard | last post by:
Hi, I've got a program whose main object contains an array of "cells", each of which contains an array of "sub-cells", each of which contains an array of "sub-sub-cells". The cells, subcells...
4
by: teddarr | last post by:
Here's my quandry.....I am building a phone book program with student and staff classes. In main() I am to define 2 arrays 1 for students and 1 for staff. Each array will hold objects. I have...
14
by: julie.siebel | last post by:
I've been wrestling with a really complex page. All the data is drawn down via SQL, the page is built via VBScript, and then controlled through javascript. It's a page for a travel company that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.