Passing an Object to a Method With a Reference Argument

A

Anon Email

Hi people,

In the following code, in the main method, why is it possible to pass
"input" as an argument to the method TheCalculator.Execute()? This
method is supposed to accept references to Input objects as arguments,
why does it accept an Input object? Is it the case that although
you've passed an object, it's interpreted as a reference?

Cheers,

Deets


#include <iostream>
using std::cout;
using std::endl;

class IStack {};

class StackSeq
{
public:
StackSeq (IStack const & stack ): _stack (stack), _done (false)
{
cout << "Stack sequencer created\n";
}
bool AtEnd () const { return _done; }
void Advance () { _done = true; }
int GetNum () const { return 13; }
private:
IStack const & _stack;
bool _done;
};

class Input
{
public:
Input ()
{
cout << "Input created\n";
}
};

class Calculator
{
public:
Calculator () : _done (false)
{
cout << "Calculator created\n";
}
bool Execute (Input & input)
{
cout << "Calculator::Execute\n";
return !_done;
}
IStack const & GetStack ()
{
_done = true;
return _stack;
}
private:
IStack _stack;
bool _done;
};

int main ()
{
Calculator TheCalculator;
bool status;
do
{
// Prompt for input
cout << "> ";
Input input;
status = TheCalculator.Execute (input);
if ( status )
{
for (StackSeq seq (TheCalculator.GetStack ());
!seq.AtEnd ();
seq.Advance () )
{
cout << " " << seq.GetNum () << endl;
}
}
} while (status);
}
 
J

John Carson

Anon Email said:
Hi people,

In the following code, in the main method, why is it possible to pass
"input" as an argument to the method TheCalculator.Execute()? This
method is supposed to accept references to Input objects as arguments,
why does it accept an Input object? Is it the case that although
you've passed an object, it's interpreted as a reference?


Yes. In fact that is the only way to pass something by reference. If you
were to pass it &input, then you would be passing a pointer to an Input
object, not a reference to an Input object.
 
J

Jorge Rivera

John said:
Yes. In fact that is the only way to pass something by reference. If you
were to pass it &input, then you would be passing a pointer to an Input
object, not a reference to an Input object.

I think he probably refers to doing something like:

Input &inputR = *someInputPtr;
status = TheCalculator.Execute(inputR);

Which I think is perfectly legal (I could be wrong, if so, somebody will
whine...).

However, you are correct in that sending the object into a function that
takes a reference will take the reference to the object, exactly the
desired behavior. (It is a lot more convenient than sending an actual
reference in a way similar to the code snippet above...).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top