473,396 Members | 1,797 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.

array of objects

AHR
Hi NG

I have a class which contains:
**********************
class felt
{
private:
int id;
int koorx;
int koory;
public:
felt(int id_x, int koorx_x, int koory_x)
{
id=id_x;
koorx=koorx_x;
koory=koory_x;
}
};
**********************

Then I have my main which should contain an array of felt-objects:
**********************
int main()
{
//Example
felt nr1(1,1,1);

??? arr[4];
arr[0]=???;
arr[1]=???;
arr[2]=???;
arr[3]=???;

return 0;
}
**********************

But how do i declare an object array? Please help me to insert the right
values on the questionmarks.

Thanks
AHR
Jul 22 '05 #1
2 1148
"AHR" <ah*@xx.xx> wrote in message
news:41*********************@dread14.news.tele.dk. ..
Hi NG

I have a class which contains:
**********************
class felt
{
private:
int id;
int koorx;
int koory;
public:
felt(int id_x, int koorx_x, int koory_x)
{
id=id_x;
koorx=koorx_x;
koory=koory_x;
}
I suggest using an intitalization list for this:

felt(int id_x, int koorx_x, int koory_x)
: id(id_x), koorx(koorx_x), koory(koory_x)
{
}
};
More below.
**********************

Then I have my main which should contain an array of felt-objects:
**********************
int main()
{
//Example
felt nr1(1,1,1);

??? arr[4];
felt arr[4];

But wait, this isn't quite enought. If you want to create
an array of user defined type, this type must be 'default-
constructible', i.e. it needs a default constructor, which
your 'felt' class does not have. You can either supply default
parameters for your contstructor, or create another constructor
which gives the data members some sensible default value (perhaps
zero?). (A 'default' constructor is one which can be invoked
with no arguments.

An example default constructor:
'
felt()
: id(0), koorx(0), koory(0)
{
}

Once you have this, you can create your array such as:

felt arr[4];
arr[0]=???;
arr[1]=???;
arr[2]=???;
arr[3]=???;
arr[0] = nr1;


return 0;
}
**********************

But how do i declare an object array? Please help me to insert the right
values on the questionmarks.


Since you have not defined any other conversions, the only thing you
can put on the right side of those assignment operators is an expression
of type 'felt', such as the name of your object 'nr1', or perhaps a
temporary object such as 'felt(1, 2, 3)'.

Having said all that, I suggest you look into using a standard container
such as 'std::vector',. which will handle all memory management for you
automatically (e.g. it can grow and shrink as needed to accomodate the
elements you want to store, this is not the case with an array).

Which C++ book(s) are you reading?

-Mike
Jul 22 '05 #2
"Mike Wahler" <mk******@mkwahler.net> wrote...
"AHR" <ah*@xx.xx> wrote in message
news:41*********************@dread14.news.tele.dk. ..
Hi NG

I have a class which contains:
**********************
class felt
{
private:
int id;
int koorx;
int koory;
public:
felt(int id_x, int koorx_x, int koory_x)
{
id=id_x;
koorx=koorx_x;
koory=koory_x;
}
I suggest using an intitalization list for this:

felt(int id_x, int koorx_x, int koory_x)
: id(id_x), koorx(koorx_x), koory(koory_x)
{
}
};


More below.
**********************

Then I have my main which should contain an array of felt-objects:
**********************
int main()
{
//Example
felt nr1(1,1,1);

??? arr[4];


felt arr[4];

But wait, this isn't quite enought. If you want to create
an array of user defined type, this type must be 'default-
constructible', i.e. it needs a default constructor, which
your 'felt' class does not have.


That's not necessarily so. Even if there is no default c-tor, you
can construct an array if you can supply all the initialisers for
the elements:

felt arr[] = { felt(1,1,1), felt(2,2,2), felt(3,3,3), felt(4,4,4) };

In this case the only requirement is that 'felt' is copy-constructible.
[...]


Victor
Jul 22 '05 #3

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

Similar topics

4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
2
by: James | last post by:
Hi, I'm hoping someone can help me out. If I declare a class, eg. class CSomeclass { public: var/func etc..... private varfunc etc..
7
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
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...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
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: heddy | last post by:
I have an array of objects. When I use Array.Resize<T>(ref Object,int Newsize); and the newsize is smaller then what the array was previously, are the resources allocated to the objects that are...
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: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
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?
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
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,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.