views:

140

answers:

8

i have two classes namely Flight and Runway. Now i am trying to pass an array of these objects as parameter to a function.

void fun(Flight ptr1[],Runway ptr2[])
{
...
...
}

ptr1 should point to an array of Flight objects and ptr2 should point to an array of Runway objects. Now inside this function fun() how do i access members of these classes. Also can i use ptr1++ or ptr2++ to move between the objects?? Also how would i be calling this func??something like this -

Flight array1[5];
Runway array2[2];
fun(array1,array2);
+1  A: 
ptr1[5].fly();
ptr2[7].run();
FredOverflow
The runway class has a member run()? :D
Armen Tsirunyan
Armen: Yes, to run away from all the planes that are trying to molest it ;-)
FredOverflow
A: 

Within the function they drop down to pointers so you can use ptr1++ or ++ptr1 and ptr2++ / ++ptr2 to move to the next item in your array.

I am not sure how your function is going to know how many items there are in the arrays as it cannot tell from the code itself so you will require some "terminator" object or to pass the size as an extra parameter.

If you (as is preferable) used std::vector then you can pass a reference or const-reference to your vector into your function and from that you can iterate through the vector instead until you reach the end iterator, and you can also ask for the size of the vector.

CashCow
A: 

You could do something like.

int i = 0;
ptr1[i].fly();
i++; // increment i to access the next element.
ptr1[i];

be careful though, you can run over the end of the array and start writing on data thats not related to the array.

PhilCK
cant i have a check condition like `while(ptr1!=NULL){ptr1++;}` to check my boundaries??
ayush
@ayush, no you can't. But you can pass additional parameters that denote these boundaries, if you know what I mean.
Armen Tsirunyan
@ayush: If you want that sort of behavior, use `std::vector`.
David Thornley
@Armen: Slightly pedantic. I added the i++ into the array index as I thought it might be what he was after with ptr1++;
PhilCK
@PhilCK: Removed the downvote
Armen Tsirunyan
A: 

use ptr1[i] where i is an offset that increments by sizeof a pointer to the Flight Class. Since arrays are not bounds-checked in C++, your function will have to somehow know the size of the array. Also, stay away from doing pointer arithmetic like ptr++, you can get into trouble that way.

Gary
A: 

In C++, the argument list

void fun(Flight ptr1[],Runway ptr2[])

is exactly equivalent to

void fun(Flight* ptr1, Runway* ptr2)

You can use the arguments like arrays, as Fred said:

ptr1[5].fly();
ptr2[7].run();

and you can also use ++ and -- because they are pointers:

Flight& FirstFlight = *ptr1;
ptr1++;
Flight& SecondFlight = *ptr1;
Flight& ThirdFlight = ptr1[1];

You cannot tell how long the array is unless you pass it as a separate argument.

Qwertie
+5  A: 
void fun(Flight ptr1[],Runway ptr2[])

is interpreted as

void fun(Flight *ptr1, Runway *ptr2)

This is called "decomposition," and I think it's rotten. It's mainly a feature for backward compatibility with C. If you want pointers, specify pointers, not arrays, because pointers and arrays are different things.

You can also do

void fun(Flight (&arr1)[5], Runway (&arr2)[7])

Now the parameters remain arrays inside the function, not pointers, so ++ arr1 is illegal and sizeof arr1/sizeof arr1[0] is 5. The argument arrays when you call the function also must be the correct size, exactly 5 and 7 respectively. In this context, & means pass-by-reference, so the arrays are not copied when you call the function.

You can leverage the template system to generate a function for any size argument array, as well:

template< size_t NF, size_t NR >
void fun(Flight (&arr1)[NF], Runway (&arr2)[NR])

Such a template may be called with any-sized arrays, and NF and NR will be integral constants set to the proper sizes.

Potatoswatter
A: 

Below is the sample program where I have defined the Flight class to demonstrate to access the member of Flight class.

#include <stdio.h>
#include <iostream>
#include <new>
using namespace std;

class Flight {
public:
    int temp;
    int temp1;

    Flight();
    void print();
};

Flight::Flight() {
    temp = 10;  
    temp1 = 11; 
}

void Flight::print() {
    printf("%d %d\n", temp, temp1);
}

void fun(Flight ptr1[]) {
    //ptr1->print();
    ptr1[0].print();
    ptr1[1].print();

    printf("temp=%d\n",ptr1[0].temp);
    printf("sizeof %d \n",sizeof(ptr1));
}

int main() {
    Flight array1[5];

    Flight *p = new Flight;
    //fun(p);
    fun(array1);

    return 0;
}

You cannot get the size of array in the fun function, and there is no way to calculate the sizeof array at runtime, so you should add extra parameter in the fun function for getting the sizeof each array.

Yes you can apply the pointer arithmetic to the Flight / Runway array.

The fun(Flight ptr1[]) is equivalent to fun(Flight *ptr1).

Dhiraj
A: 

Don't use arrays like this, unless there is no other way. Use std::vector instead:

void fun( const std::vector<Flight> &flights, const std::vector<Runway> &runways)
{
...
...
}

And to call the function:

std::vector<Flight> flights(5);
std::vector<Runway> runways(2);
fun(array1,array2);

Much better this way.

rubenvb
Much better is to pass those vectors by const reference...
Alexandre C.
Whoa, speed bug :). I normally do it that way... (kicks himself in he butt)
rubenvb