Archived

This topic is now archived and is closed to further replies.

viper35al

& operator question

Recommended Posts

viper35al    122
In my C++ book, I hav the following code. My question is in comment form next to the area I have a question in. Thanks in advance.
  

//Listing 9.9

// Returning multiple values from a function

// using references


#include <iostream.h>

typedef unsigned short USHORT;
enum ERR_CODE { SUCCESS, ERROR };

ERR_CODE Factor(USHORT, USHORT&, USHORT&); // In my book it 

// talked about the & operator before the variable to show its

// adress in memory, or to create a reference, but wat does it 

// do when put after like in this case, I re read my book and 

// couldn''t find it saying anything about this.


int main()
{
	USHORT number, squared, cubed;
	ERR_CODE result;

	cout << "Enter a number (0 - 20): ";
	cin >> number;

	result = Factor(number, squared, cubed);

	if (result == SUCCESS)
	{
		cout << "number: " << number << "\n";
		cout << "square: " << squared << "\n";
		cout << "cubed: "  << cubed   << "\n";
	}
	else
		cout << "Error encountered!!\n";
	return 0;
}

ERR_CODE Factor(USHORT n, USHORT &rSquared, USHORT &rCubed)
{
	if (n > 20)
		return ERROR;   // simple error code

	else
	{
		rSquared = n*n;
		rCubed = n*n*n;
		return SUCCESS;
	}
}

  

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
It is the reference operator -> it allows you to reference (much like a pointer, just you can''t change the address it points to and syntax is the same as a normal var) a variable so that you can edit its contents (or even just decrease overhead/keep a copy constructor from firing since you are only really passing the address)

Share this post


Link to post
Share on other sites
ZealousElixir    256
You know what the line is, right? It''s a function prototype. The fact that the &''s are after the type names indicate that those parameters are references to the type. Keep in mind that you don''t have to give names to parameters in the function header (you don''t ever technically have to, but in order to use them you do).

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[if you have a link proposal, email me.]

Share this post


Link to post
Share on other sites
Sneftel    1788
The meaning is exactly the same, actually.

Ya see, when you''re forward-declaring function prototypes like they''ve done there, you don''t actually have to tell the compiler what the argument names are. All it needs to know are the datatypes.

So what you could actually read that line as, is:


  
ERR_CODE Factor(USHORT myFirstArg, USHORT &mySecondArg, USHORT &myThirdArg);


And the meaning--that the argument is pass-by-reference--is the same. I personally don''t see why anyone would use this ambiguous and antiquated syntax, as having a spot to describe interface where it''s needed should be just gravy; but to each his own.

Share this post


Link to post
Share on other sites
ZealousElixir    256
Another note, which you should already know about. By passing those parameters as references, you can modify their values, removing the necessity to return anything (and allowing you to effectively "return" multiple values, since the changes you apply to the parameters are actually being applied on the memory location, which, as AP mentioned, also avoids a copy constructor call.)

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[if you have a link proposal, email me.]

Share this post


Link to post
Share on other sites
viper35al    122
ok, I understand what was happening there pretty well, thanks for all your help, but I have one more example from my book that isn''t passing anything in, but still has the & operator.

Thanks.


  


//Listing 9.12

// Passing references to objects


#include <iostream.h>

class SimpleCat
{
public:
SimpleCat();
SimpleCat(SimpleCat&);
~SimpleCat();

int GetAge() const { return itsAge; }
void SetAge(int age) { itsAge = age; }

private:
int itsAge;
};

SimpleCat::SimpleCat()
{
cout << "Simple Cat Constructor...\n";
itsAge = 1;
}


// I don''t understand why there''s a & operator here, it''s not

// being passed a value. Can anyone please explain what it''s

// doing?


SimpleCat::SimpleCat(SimpleCat&)
{
cout << "Simple Cat Copy Constructor...\n";
}

SimpleCat::~SimpleCat()
{
cout << "Simple Cat Destructor...\n";
}

const SimpleCat & FunctionTwo (const SimpleCat & theCat);

int main()
{
cout << "Making a cat...\n";
SimpleCat Frisky;
cout << "Frisky is " << Frisky.GetAge() << " years old\n";
int age = 5;
Frisky.SetAge(age);
cout << "Frisky is " << Frisky.GetAge() << " years old\n";
cout << "Calling FunctionTwo...\n";
FunctionTwo(Frisky);
cout << "Frisky is " << Frisky.GetAge() << " years old\n";
return 0;
}

// functionTwo, passes a ref to a const object

const SimpleCat & FunctionTwo (const SimpleCat & theCat)
{
cout << "Function Two. Returning...\n";
cout << "Frisky is now " << theCat.GetAge();
cout << " years old \n";
// theCat.SetAge(8); const!

return theCat;
}

Share this post


Link to post
Share on other sites
myme15    122
Your example is a copy constructor. A copy constructor is called every time your program needs to make a copy of your object. If you pass a SimpleCat to a function by value (as opposed to passing a pointer or a reference), the function needs to make its own local copy of the SimpleCat. At this point, the copy constructor is automatically called, and a reference to the object that needs to be copied is passed in. The copy constructor then does whatever it needs to to copy the object.

I''m pretty bad at explaining things, aren''t I.




Things are not what they are.

Share this post


Link to post
Share on other sites
viper35al    122
I think I got it, so the copy constructor is called each time a copy of the object is made, but I deleted the whole copy constructor, and it worked fine, so is the only reason to use one in this situation is for the author to show me how to use one if I ever needed it.

If I'm wrong, please tell me, but otherwise thanks for all of your help.



[edited by - viper35al on May 6, 2002 10:19:54 PM]

Share this post


Link to post
Share on other sites
Neko-    122
The compiler makes a default copy constructor.. that one in your code is just to show you when it is used by printing a msg.

[edited by - Neko- on May 7, 2002 2:17:14 AM]

Share this post


Link to post
Share on other sites