Passing by reference - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Passing by reference

PooBearPooBear Posts: 19Member
[b][red]This message was edited by PooBear at 2006-2-1 23:59:35[/red][/b][hr]
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



Comments

  • stoberstober Posts: 9,765Member ✭✭✭
    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() ?

    [code]
    #include

    using namespace std;
    void foo(int n)
    {
    cout << "foo: oritinal value of n = " << n << endl;
    n = 1234;
    cout << "foo: new value of 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;
    }
    [/code]
  • PooBearPooBear Posts: 19Member
    [b][red]This message was edited by PooBear at 2006-2-2 8:50:7[/red][/b][hr]
    : 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() ?
    :
    : [code]
    : #include
    :
    : using namespace std;
    : void foo(int n)
    : {
    : cout << "foo: oritinal value of n = " << n << endl;
    : n = 1234;
    : cout << "foo: new value of 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;
    : }
    : [/code]
    :

    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.





  • HK_MP5KPDWHK_MP5KPDW Posts: 770Member ✭✭✭
    : : 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() ?
    : :
    : : [code]
    : : #include
    : :
    : : using namespace std;
    : : void foo(int n)
    : : {
    : : cout << "foo: oritinal value of n = " << n << endl;
    : : n = 1234;
    : : cout << "foo: new value of 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;
    : : }
    : : [/code]
    : :
    :
    : 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.
    :
    :
    [blue]
    Well, it's easy enough to test where a copy gets made:
    [code]
    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;
    }
    [/code]

    My output:
    [code]
    Default constructor called.
    In main, calling function func1.
    In function func1, calling function func2.
    Copy constructor called.
    In function func2.
    [/code]
    [/blue]

  • stoberstober Posts: 9,765Member ✭✭✭
    :
    : 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()
  • PooBearPooBear Posts: 19Member
    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()
    :

  • DonotaloDonotalo Posts: 715Member
    : 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
    :
    [purple]
    u can think reference just as [b]another name[/b] of the same variable. so the function [blue]bar(int& n)[/blue] takes a reference to another variable means [blue]n[/blue] inside [blue]bar()[/blue] is another name of the variable that is passed through [blue]n[/blue]. with an unary variable n, [b]*n[/b] has no meaning, unless n is a pointer. since [blue]&n[/blue] means a reference (ie, another name of same variable), not a pointer inside [blue]bar()[/blue], the code above has syntax error.
    [/purple]
    [hr][purple]~Donotalo()[/purple]

Sign In or Register to comment.