pass by reference

Discussion in 'C++' started by thomas, Apr 9, 2008.

  1. thomas

    thomas Guest

    hi guys, I got a question about "pass by reference".

    I want to know if I can pass a reference handler of a stack object to
    a function as parameter?
    ----code---
    void func(classtype &x);
    classtype x;
    func(x);
    -----code---

    Will this work? seems that the "x" object is in stack
    Can "func" know where to find the "x" object?
    thomas, Apr 9, 2008
    #1
    1. Advertising

  2. thomas

    thomas Guest

    On Apr 9, 10:19 pm, "Victor Bazarov" <> wrote:
    > thomas wrote:
    > > hi guys, I got a question about "pass by reference".

    >
    > > I want to know if I can pass a reference handler of a stack object to
    > > a function as parameter?

    >
    > Remove the words "a reference hanlder", replace "stack" with "local",
    > and you have a better (closer to idiomatic language) statement; and
    > the answer is "yes, you can".
    >
    > > ----code---
    > > void func(classtype &x);
    > > classtype x;
    > > func(x);

    >
    > I do sincerely hope you mean the previous statements are inside some
    > function in your program.
    >
    > > -----code---

    >
    > > Will this work?

    >
    > Yes.
    >
    > > seems that the "x" object is in stack
    > > Can "func" know where to find the "x" object?

    >
    > What do you mean by "where to find"?  Your function has a reference
    > to the object.  It uses the reference to "find" the object, so to
    > speak, just like if you would declare the reference to you 'x' in
    > the same scope as 'x' itself.  The reference becomes the object's
    > alias, a way for the function to get to the object, its members, and
    > even its location if you so desire (taking the address of a reference
    > yields the address of the object).  You can verify that you get the
    > same object by printing the address inside the function and outside:
    >
    >     #include <iostream>
    >     #include <ostream>
    >
    >     class classtype {};
    >
    >     void foo(classtype& r) {
    >         std::cout << "inside: " << &r << std::endl;
    >     }
    >
    >     int main() {
    >         classtype x;
    >         std::cout << "outside: " << &x << std::endl;
    >         foo(x);
    >     }
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    I accept your advice and thank you for your efforts.
    thomas, Apr 10, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jerry
    Replies:
    20
    Views:
    7,893
    Roedy Green
    Sep 9, 2005
  2. blufox
    Replies:
    2
    Views:
    538
  3. Mr A
    Replies:
    111
    Views:
    2,060
  4. Robert
    Replies:
    10
    Views:
    1,356
    E. Robert Tisdale
    Aug 24, 2005
  5. LuB
    Replies:
    6
    Views:
    442
    Mike Wahler
    Sep 23, 2005
Loading...

Share This Page