Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,075 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,784 people online right now. Registration is fast and FREE... Join Now!




creating an array of objects

 
Reply to this topicStart new topic

creating an array of objects

didgy58
2 Feb, 2008 - 03:22 AM
Post #1

D.I.C Head
**

Joined: 23 Oct, 2007
Posts: 217



Thanked: 1 times
My Contributions
hi guys i have my C++ code in which i have created a new class and in this class i define the variables and the strucutre of a coil, so when the user clicks the left mouse button on the document it draws a coil, ive also got this to save using the serialize, now i would like to be able to create and array of this coil so when the user draws more than one it saves them all not just the last one drawn,

would i go about creating an array like this

CODE


MyCoil mycoil[32]    //my coil being the reference to that class. and being that the maximum number of coils that can be drawn is 32 at anytime.
int n=0;


from there how would i go about creating a coil like so? when the user clicks the left mouse button


CODE

n=n+1
mycoil[n-1].x=point.x;
mycoil[n-1].y=point.y;

invalidate()



and then in on draw so when the screen is refreshed it draws it all automatically

CODE


for (1=0; i<n){

mycoil[i].x;
mycoil[i].y;

}


and would i then use the same kind of loop when im saving the item??

im kind of lost on the way to work the array. so if anybody has any good ideas or any sort of good way of describing how i should implement this i would be very greatful

thanks

dan
User is offlineProfile CardPM
+Quote Post

Bench
RE: Creating An Array Of Objects
2 Feb, 2008 - 03:37 AM
Post #2

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 686



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
Your comment for the array is wrong. When you create an array, you get an array of 32 objects. The object creation has already taken place, using the MyCoil default constructor.
User is offlineProfile CardPM
+Quote Post

didgy58
RE: Creating An Array Of Objects
2 Feb, 2008 - 06:13 AM
Post #3

D.I.C Head
**

Joined: 23 Oct, 2007
Posts: 217



Thanked: 1 times
My Contributions
ok then how would i then go about creating an array that youre not sure of how many objects there would be? would i have to create a variable like n that counts everytime the user has clicked the mouse button and then put that in the brackets instead??

maybe even use the n that i have created already?? would this work?? thanks



User is offlineProfile CardPM
+Quote Post

Bench
RE: Creating An Array Of Objects
2 Feb, 2008 - 06:53 AM
Post #4

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 686



Thanked: 24 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(didgy58 @ 2 Feb, 2008 - 02:13 PM) *

ok then how would i then go about creating an array that youre not sure of how many objects there would be? would i have to create a variable like n that counts everytime the user has clicked the mouse button and then put that in the brackets instead??

maybe even use the n that i have created already?? would this work?? thanks

Arrays are tricky to use when you need to resize them, with loads of hidden gotcha's (This is why use of "raw" arrays are heavily discouraged). in C++, you should use one of the STL containers (such as a vector, which is like a "C++ array"), then you can add as many or as few objects as you like.

you might start out with something like
CODE
#include <iostream>
#include <vector>

int main()
{
    //Create an empty vector
    std::vector<MyCoil> mycoil;

    MyCoil coil1;
    MyCoil coil2;

    //Add coil1 and coil2 objects to the vector
    mycoil.push_back( coil1 );
    mycoil.push_back( coil2 );

    std::cout << "There are " << mycoil.size()
              << " objects in mycoil";
}

For element access, you can use the [ ] subscript operator as with an array, although the subscript operator only works with existing elements. functions like push_back allow the vector to be resized.

Here's some reference material for vector, with examples http://www.cppreference.com/cppvector/index.html

This post has been edited by Bench: 2 Feb, 2008 - 06:59 AM
User is offlineProfile CardPM
+Quote Post

Jingle
RE: Creating An Array Of Objects
2 Feb, 2008 - 02:04 PM
Post #5

D.I.C Regular
***

Joined: 20 Oct, 2007
Posts: 250


My Contributions
QUOTE(Bench @ 2 Feb, 2008 - 07:53 AM) *


Here's some reference material for vector, with examples http://www.cppreference.com/cppvector/index.html

thank you thats very useful
just what iv been looking for.


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 11:08PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month