0

Possible Duplicate:
How to pass objects to functions in C++?

My main program gives me coefficient of plane equation as a vector.

I have many planes and I am trying to find out their intersections. So, I have series of plane parameters, belongs to each plane and I have created a map to accommodate plane parameters and I gave a key as plane number. Now, I am calling planes according to randomly given order (that is why, I use map). So, for each time I am using two planes to compare whether they intersect or not.

My function:

vector<double> intersection_vector(vector<double> plane1,vector<double> plane2){
    vector<double> cross_product;
    double a1=plane1.at(0);
    double b1=plane1.at(1);
    double a2=plane2.at(0);
    double b2=plane2.at(1);
    //double d1=plane1.at(2);
    //double d2=plane2.at(2);
    int c1=-1,c2=-1;
    double cross_a=(b1*c2)-(b2*c1);
    double cross_b=(a2*c1)-(a1*c2);
    double cross_c=(a1*b2)-(a2*b1);
    cross_product.push_back(cross_a);cross_product.push_back(cross_b);cross_product.push_back(cross_c);
    return cross_product;
    }

Main program:

//some codes

map<int, vector<int> >::iterator xx;
for (xx=parameter_list.begin();xx!= parameter _list.end();xx++){
       int my_plane=xx->first;
       vector <int> your_planes=xx->second;
       for(int i=0; i< your _planes.size();i++){        
                   int any_ your _plane= your _planes.at(i);
        vector <double>edge_line=intersection_vector(my_plane, any_ your _plane);

       // do something

Now, I want to use pointers and references to speed up this process. Please help me while explaining why use * and & for respective places. (Maybe I need call by reference or something else and what I did is call by value? Is that, plase explain because, I do not have basic on this).

Community
  • 1
  • 1
niro
  • 947
  • 3
  • 13
  • 30

2 Answers2

3

If you want to learn when to use pointers or references I strongly recommend you read "Effective C++" and "More Effective C++" by Scott Meyers. They are both excellent books that will give you a deep understanding of how to write good C++ code.

I'm sure they're available on Amazon.

Cameron Skinner
  • 51,692
  • 2
  • 65
  • 86
  • ok, thank you. i will look it. but, if you can please just modify this code as a guidance for me. – niro Mar 31 '11 at 02:37
  • In this particular case it probably doesn't matter too much whether you pass references or pointers. Passing values is bad. I'd suggest you change the signature to `void intersection_vector(const vector& plane1, const vector& plane2, vector& result)`. This means you'll be passing in an out-parameter, and using references guarantees you won't get a null. – Cameron Skinner Mar 31 '11 at 02:45
  • thank you very much. one more thing. why you did you use "const" before input parameters? any hint plz. also, when i call this function, would it be how? – niro Mar 31 '11 at 02:49
  • `const` tells the compiler that the contents of those vectors will not be altered. It's a good way to help you write correct code and sometimes it allows the compiler to make optimisations that it otherwise could not. Calling the function will be much the same as you already have, but with the extra output argument: `vector result; intersection_vector(my_plane, any_your_plane, result);` – Cameron Skinner Mar 31 '11 at 02:56
  • thank you. yes, i never use const and now know the importancy of it – niro Mar 31 '11 at 08:29
0

At the very least your should pass the intersection_vector parameters by reference. Also it's probably not a good idea to return a vector by value from intersection_vector.

Kevin
  • 24,871
  • 19
  • 102
  • 158