question about pointer-parameter

Hi all,

In my program I have a function like:

1
2
3
int dfsArea(Rectangle * currentRect, int n, vector<Rectangle*> myvector, int & area, Rectangle * root){
...
}

In the function I do some union operations to the elements of myvector and I want the parameters except n to be modified. Do I need to use call by reference or something, or is it okay since I pass them with pointers? How should I write the code if it needs to be called by reference?

Thanks in advance.
Last edited on
The question is whether you want to modify the pointers themselves or just the objects they point to.
myvector[something] will return a reference to the pointer, though if you can you should pass myvector by reference (or even better, const reference) to avoid copying it.
I think I should post more of the code. Rectangle class is:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "Rectangle.h"

Rectangle::Rectangle(){

	this->parent=this;
	this->area=0;
	this->x1=0;
	this->x2=0;
	this->y1=0;
	this->y2=0;
	this->visited=false;

}

and functions:
1
2
3
4
5
6
7
8
9
/*
 *  recursive method to find the root of a rectangle object
 */
Rectangle* find(Rectangle *x){
	if(x->parent == x)
			return x;
	else
		return find(x->parent);
}

1
2
3
4
5
6
7
8
/*
 *  method to merge two sets if they are not already in the same set
 */
void unionSets(Rectangle * x, Rectangle * y){ 

	y->parent=x;

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int dfsArea(Rectangle * currentRect, int n, vector<Rectangle*> myvector, int & area, Rectangle * root){

	currentRect->visited=true;
	area+=(currentRect->x2-currentRect->x1)*(currentRect->y2-currentRect->y1);

	if(currentRect->parent!=currentRect){
		area-=(min(currentRect->x2, currentRect->parent->x2)-max(currentRect->x1, currentRect->parent->x1))*(min(currentRect->y2, currentRect->parent->y2)-max(currentRect->y1, currentRect->parent->y1));
	}

	// for each rect intersecting with currentRect
	for(int k=0; k<n ; k++){
		if(myvector[k]->visited==false && currentRect->x2 > myvector[k]->x1 && currentRect->x1 < myvector[k]->x2 && currentRect->y2 > myvector[k]->y1 && currentRect->y1 < myvector[k]->y2)
		{
			unionSets(currentRect, myvector[k]);
			area+=dfsArea(myvector[k], n, myvector, area, root);
		}
		else if(find(myvector[k])==root && currentRect!=myvector[k]){ // &root?
			area+=(currentRect->x2-currentRect->x1)*(currentRect->y2-currentRect->y1);
			area-=(min(currentRect->x2, currentRect->parent->x2)-max(currentRect->x1, currentRect->parent->x1))*(min(currentRect->y2, currentRect->parent->y2)-max(currentRect->y1, currentRect->parent->y1));
			area-=(min(currentRect->x2, myvector[k]->x2)-max(currentRect->x1, myvector[k]->x1))*(min(currentRect->y2, myvector[k]->y2)-max(currentRect->y1, myvector[k]->y1));

		}
	}
	return area;
}

and in main i call dfsArea like this:
1
2
3
4
5
6
....
vector<Rectangle*> myvector;
....
....
myvector[seq]->area=dfsArea(myvector[seq], n, myvector, area, myvector[seq]);
....


So I have a vector with pointers to rectangle objects, and in dfsarea function which is recursive the areas of the groups of intersecting rectangles are computed by doing union operations on them.
Should I modify the pointers or the objects they point to then? And how?
Last edited on
Topic archived. No new replies allowed.