|
-
Nov 19th, 2002, 07:50 PM
#1
Thread Starter
Addicted Member
arrays of objects
I want to declare a global array of objects, but i don't know how many objects there will be until later in the program. how would i do this?
thanks
-
Nov 19th, 2002, 09:09 PM
#2
transcendental analytic
you declare a pointer to the object.
to allocate an deallocate memory dynamically you use new and delete.
ex
obj* g_myobjarray;
..
g_myobjarray=new obj[x];
..
delete[] g_myobjarray;
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Nov 20th, 2002, 05:47 AM
#3
Or you declare an STL vector, which is easier to use.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 20th, 2002, 09:28 PM
#4
Thread Starter
Addicted Member
Passing Arrays Of Objects ByRef
say i do something like this:
Obj *myObj;
myObj = new Obj[10];
Now, myObj is a pointer right? So if I wanted to pass the array byref to a function, would i have to pass the memory address of the pointer myObj to a function that accepted the value as another pointer? Like:
void blah(Obj *pointer) {}
blah(&myObj)
or could I just pass the pointer myObj and not the mem address? Also, is there any way for 'blah' to tell the upper bound of the array that was passed? What is the correct argument syntax for passing an array?
thanks
jmiller
-
Nov 21st, 2002, 05:45 AM
#5
transcendental analytic
I suggest you do as CB suggested and have a look at the vector if you want a fast and easy solution, if you're practicing C++ arrays, pointers and references can be very useful to understand
myObj is a pointer to the first object in the array, if you want to reassign or resize the array you'd better pass the pointer by reference, Obj *&pointer, and when you declare a reference, you don't have to use the addressof operator, so
void blah(Obj *&pointer) {}
..
blah(myObj);
if you only want to change or access of the array elements, you could just pass the pointer, but you cannot change the pointer (you can but only in this scope.)
void blah(Obj *pointer) {}
..
blah(myObj);
The size of the array has to be passed as well in a separate variable, if you intend to resize it. Remember for each new you have to use delete, and be cautious to new and delete in separate scopes, you'll easily end up with memory leaks, its not considered as good programming to do so.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|