C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Passing by reference Posted by PooBear on 1 Feb 2006 at 9:49 PM
This message was edited by PooBear at 2006-2-1 23:59:35

This is for Linux C++ (gcc compiler)

I have two functions which takes the parameters by refernce. It should be something like this.

The function "ProcessRequest()" in Class Process call the "Process()"
function of the foo class inside of it which has its argument passed by reference.
1. How do I pass n1 to "Process()" function.

2. What if I change Foo class Process( first &p1) function takes the parameter by value like this Process( first p1) and call just Like what I am doing in the function ProcessRequest() in class Precess.
Does that make copies?

Please some one explain to me.


class first
{
....
};

class second
{
....
};

class Foo
{
int Process(first &p1)
{
.....
}
}

class Process
{
Foo data;

......

int ProcessRequest (first &n1 , second & n2)
{
data.Process(n1);
}
};


Thanks
Machado



Report
Re: Passing by reference Posted by stober on 2 Feb 2006 at 6:23 AM
not quite sure what you are asking -- something along these lines? Where an int is passed to bar() by reference, and bar() passes it by value to foo() ?

#include <iostream>

using namespace std;
void foo(int n)
{
     cout << "foo:  oritinal value of n = " << n << endl;
     n = 1234;
     cout << "foo:  new value of n = " << n << "\n" << endl;
}

void bar(int& n)
{
     n = 2;
     cout << "bar: original value of n = " << n  << endl;
     foo(n);
     cout << "bar: new value of n = " << n  << endl;
}

int main(int argc, char *argv[])
{
    int n = 3;
    bar(n);
    system("PAUSE");
    return EXIT_SUCCESS;
}

Report
Re: Passing by reference Posted by PooBear on 2 Feb 2006 at 8:49 AM
This message was edited by PooBear at 2006-2-2 8:50:7

: not quite sure what you are asking -- something along these lines? Where an int is passed to bar() by reference, and bar() passes it by value to foo() ?
:
:
: #include <iostream>
: 
: using namespace std;
: void foo(int n)
: {
:      cout << "foo:  oritinal value of n = " << n << endl;
:      n = 1234;
:      cout << "foo:  new value of n = " << n << "\n" << endl;
: }
: 
: void bar(int& n)
: {
:      n = 2;
:      cout << "bar: original value of n = " << n  << endl;
:      foo(n);
:      cout << "bar: new value of n = " << n  << endl;
: }
: 
: int main(int argc, char *argv[])
: {
:     int n = 3;
:     bar(n);
:     system("PAUSE");
:     return EXIT_SUCCESS;
: }
: 

:

Thanks!! Yes what you are doing is correct. I want to know in function "void foo(int n)" you are passing it by value does that make copyies even though you are passing the argument by reference in "void bar(int& n)"
thats what I want to know.





Report
Re: Passing by reference Posted by HK_MP5KPDW on 2 Feb 2006 at 11:34 AM
: : not quite sure what you are asking -- something along these lines? Where an int is passed to bar() by reference, and bar() passes it by value to foo() ?
: :
: :
: : #include <iostream>
: : 
: : using namespace std;
: : void foo(int n)
: : {
: :      cout << "foo:  oritinal value of n = " << n << endl;
: :      n = 1234;
: :      cout << "foo:  new value of n = " << n << "\n" << endl;
: : }
: : 
: : void bar(int& n)
: : {
: :      n = 2;
: :      cout << "bar: original value of n = " << n  << endl;
: :      foo(n);
: :      cout << "bar: new value of n = " << n  << endl;
: : }
: : 
: : int main(int argc, char *argv[])
: : {
: :     int n = 3;
: :     bar(n);
: :     system("PAUSE");
: :     return EXIT_SUCCESS;
: : }
: : 

: :
:
: Thanks!! Yes what you are doing is correct. I want to know in function "void foo(int n)" you are passing it by value does that make copyies even though you are passing the argument by reference in "void bar(int& n)"
: thats what I want to know.
:
:

Well, it's easy enough to test where a copy gets made:
class object
{
public:
    object()
    {
        cout << "Default constructor called." << endl;
    }
    object(const object& obj)
    {
        cout << "Copy constructor called." << endl;
    }
};

void func2(const object obj) // By value
{
    cout << "In function func2." << endl;
}

void func1(const object& obj) // By reference
{
    cout << "In function func1, calling function func2." << endl;
    func2(obj);
}

int main()
{
    object obj;

    cout << "In main, calling function func1." << endl;

    func1(obj);

    return 0;
}


My output:
Default constructor called.
In main, calling function func1.
In function func1, calling function func2.
Copy constructor called.
In function func2.



Report
Re: Passing by reference Posted by stober on 2 Feb 2006 at 11:39 AM
:
: Thanks!! Yes what you are doing is correct. I want to know in function "void foo(int n)" you are passing it by value does that make copyies even though you are passing the argument by reference in "void bar(int& n)"
: thats what I want to know.
:


yes -- its just a copy in function foo()
Report
Re: Passing by reference Posted by PooBear on 2 Feb 2006 at 10:25 PM
Thanks
What if I have the functions like this
void foo(int & n)
and the second function call this inside of it
like
void bar(int& n)
{
foo( *n ); // How do I pass this
}

In this case, I believe it will not make any copies.
I want to know does this work ?

Thanks


: :
: : Thanks!! Yes what you are doing is correct. I want to know in function "void foo(int n)" you are passing it by value does that make copyies even though you are passing the argument by reference in "void bar(int& n)"
: : thats what I want to know.
: :
:
:
: yes -- its just a copy in function foo()
:

Report
Re: Passing by reference Posted by Donotalo on 2 Feb 2006 at 11:27 PM
: Thanks
: What if I have the functions like this
: void foo(int & n)
: and the second function call this inside of it
: like
: void bar(int& n)
: {
: foo( *n ); // How do I pass this
: }
:
: In this case, I believe it will not make any copies.
: I want to know does this work ?
:
: Thanks
:

u can think reference just as another name of the same variable. so the function bar(int& n) takes a reference to another variable means n inside bar() is another name of the variable that is passed through n. with an unary variable n, *n has no meaning, unless n is a pointer. since &n means a reference (ie, another name of same variable), not a pointer inside bar(), the code above has syntax error.


~Donotalo()




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.