public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* Pass by reference problem
@ 2009-02-17  3:29 e211
  2009-02-18 22:01 ` Andrew Pinski
  0 siblings, 1 reply; 2+ messages in thread
From: e211 @ 2009-02-17  3:29 UTC (permalink / raw)
  To: gcc-bugs


//The following code works and there is no way it should.  Seems like a bug
someone put in on purpose

#include <iostream>
using namespace std;

void swap(int *x, int *y)
{
	int temp;
	temp = *x;
	*x = *y;
	*y = temp;
}

int main()
{
	int x = 10;
	int y = 20;
	cout << x << " " << y << endl;
	swap(x,y);  //how does this work, there is no way this should compile and
run and actually work
	cout << x << " " << y << endl;
}

-- 
View this message in context: http://www.nabble.com/Pass-by-reference-problem-tp22049211p22049211.html
Sent from the gcc - bugs mailing list archive at Nabble.com.


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Pass by reference problem
  2009-02-17  3:29 Pass by reference problem e211
@ 2009-02-18 22:01 ` Andrew Pinski
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Pinski @ 2009-02-18 22:01 UTC (permalink / raw)
  To: e211; +Cc: gcc-bugs

On Mon, Feb 16, 2009 at 7:29 PM, e211 <e211@hotmail.com> wrote:
>
> //The following code works and there is no way it should.  Seems like a bug
> someone put in on purpose
>
> #include <iostream>
> using namespace std;
>
> void swap(int *x, int *y)
> {
>        int temp;
>        temp = *x;
>        *x = *y;
>        *y = temp;
> }
>
> int main()
> {
>        int x = 10;
>        int y = 20;
>        cout << x << " " << y << endl;
>        swap(x,y);  //how does this work, there is no way this should compile and
> run and actually work
>        cout << x << " " << y << endl;
> }

iostream is bringing in the definition of std::swap which takes a
reference.  And you have a "using namespace std;" there.

Thanks,
Andrew Pinski


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2009-02-18 22:01 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-02-17  3:29 Pass by reference problem e211
2009-02-18 22:01 ` Andrew Pinski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).