0 Replies - 363 Views - Last Post: 20 October 2011 - 10:41 PM Rate Topic: -----

Topic Sponsor:

#1 dvoiddofunk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-October 11

reference initialization question

Posted 20 October 2011 - 10:41 PM

Hi, i'm learning on my own from a C++ book ("teach yourself C++ in 24 hours"). I understood the book's first statements around initializing a reference. The first example given is;

int howBig = 200;
int &rIntRef = howBig;



ok, that's easy and straightforward. But then I am confused when a reference is being used in a program. there's no single two statements that look like that in the reference. Here's the code:

void swap(int &rt, int &ry, int &rz) 
{
	int temp;          
	std::cout << "Swap. before swap, rt: " << rt << " ry " << ry << " rz: " << rz << "\n";
	
	temp = rt;
	rt = ry; 
	ry = rz;
	rz = temp;

	std::cout << "swap. after swap, rt: " << rt << " ry: " << ry << " rz: " << rz << "\n";
	
}



this code is part of a larger program that compiled and ran just fine. But I don't understand how the reference is being initialized. For example, the declaration around void swap(int &rt...). is this the declaration?

int &rt;
int temp;
temp = rt;



if so, then when commented out the temp = rt; line, the compiler should have flagged that as an error. but it didn't. so now i don't know what's being referenced. Thanks for any clarification.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1