Doug
Doug

Reputation: 1316

Passing a Class Object to a function (probably by pointer not reference) C++

So let's say I have two different functions. One is a part of the BST class, one is just a helper function that will call on that Class function. I will list them out here.

sieve(BST<T>* t, int n);

this function is called like this: sieve(t,n) the object is called BST t;

I'm going to be using the class remove function within the sieve function to remove specific objects. I'm not sure what my prototype for this basic function should look like? Doing this:

sieve(BST<int> t, int n)

What happens here is everything compiles just fine, but when t.remove function is called I see no actual results. I'm assuming because it's just creating a copy or a whole other t object instead of passing the one from my main() function.

If I call the remove function (t.remove(value)) in my main function where the original object was created it removes everything properly. Once I start doing it through my sieve function I see no change when I re print it out from my main function. So my main function looks something like this:

int main ()
{
   int n,
       i,
       len;

   BST<int> t;

   cin >> n;
   vector<int> v(n);

   srand(1);

   for (i = 0; i < n; i++)
      v[i] = rand() % n;

   for (i = 0; i < n; i++)
      t.insert(v[i]);
       print_stat(t);
   t.inOrder(print_data);



   sieve(v,t,n);
   print_stat(t);
   t.inOrder(print_data);
   return 0;
}

So my results end up being the same, even though my debug statements within the functions show it's actually deleting something. I'm guessing where I'm going wrong is how I am passing the t object onto the function.

Upvotes: 1

Views: 3005

Answers (4)

andy.gurin
andy.gurin

Reputation: 3914

If I understand correctly your issue, you should use

BST t;    
sieve(BST<T> *t, int n);

and invoke it as:

sieve(&t,n)

passing the pointer to the t object

OR

BST t; 
sieve(BST<T> &t, int n);

and invoke it as:

sieve(t,n)

passing the reference to the t object

Upvotes: 3

Dima
Dima

Reputation: 39389

What happens here is everything compiles just fine, but when t.remove function is called I see no actual results. I'm assuming because it's just creating a copy or a whole other t object instead of passing the one from my main() function.

Correct. That is exactly what happens, because in C++ parameters are passed to funcitons by value. Passing a reference or a pointer will fix your problem. Using a reference is cleaner.

Upvotes: 1

Michael Burr
Michael Burr

Reputation: 340178

The signature:

/* missing return type */ sieve<BST<int> t, int n);

will, in fact, make a copy of the BST<int> passed in to sieve(). So any changes you make to it will be thrown away (unless you return a copy of it).

You probably want something like:

void sieve<BST<int> & t, int n);

which passes in a reference, so any modifications you make to t inside the method are made to the object that you passed in (not a copy).

Once you get that down and understand it, you'll probably want to make the sieve() function a "function template" so that it can take a BST<> containing any type.

Upvotes: 2

C. K. Young
C. K. Young

Reputation: 222973

sieve(BST<int>& t, int n)

The & specifies passing by reference rather than value. :-)

Upvotes: 3

Related Questions