I am self learning c++. I am confused as to object array and whether the number in the square brackets refers to the actual number of objects.
IE: Plane sopwith [4];
Does this mean there are 4 identical sopwith objects belonging to the class Plane?
If this is the case, then is each sopwith object then automatically numbered from 0 to 3?
Thankyou very much for those fast, informative replies.
Is it possible then, for the class Plane to have several of those object arrays?
ie; Plane sopwith [4] and Plane sopwithPup [8] and perhaps even more... say... sopwithCamel[10] etc?
Is it possible then, for the class Plane to have several of those object arrays?
You've got a bit wrong way. The class Plane does not "have" the objects (or arrays of objects) created from its description. Instead, you should think of objects as variables, such as int, float, etc.. Variables and objects are "owned" by the context:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Plane B52; // This is a global object, destroyed upon program exit
int i; // This is a global variable, destroyed upon program exit
int Function(void)
{
Plane Airbus[4]; // These are 4 Airbus planes "owned" by the Function() and destroyed upon "return 0;"
if (...) {
Plane Jumbo;
// ....
} // Jumbo is owned by if()'s context and is destroyed here
return 0;
}
That said, there is not much difference in how a regular variable (int, float, ..) and objects (Plane, ...) are treated.
It is better to pass the array by reference (Plane &PlaneArr[]). If you pass the array by copy (Plane PlaneArr[]), the compiler will have to do a copy operation to every Plane in that array, and this:
- Might take a lot of processing time.
- Will leave the original array unchanged because the copy of the array is modified.
If you have a function that is not supposed to change anything in the array, pass it by const reference.
The intention is that the object array goes to a function which may alter whatever was sent. ie; if sopwith[0] vs baron[3] are sent to the battle function its class attributes are supposed to be accessed and damage updated to either instance as required.
ie (pseudo)