Passing by Reference

This is a discussion on Passing by Reference within the C++ Programming forums, part of the General Programming Boards category; I have been passing data into methods by reference as means of returning results. However, I've been running into problems ...

  1. #1
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812

    Passing by Reference

    I have been passing data into methods by reference as means of returning results. However, I've been running into problems & suspect I maybe misusing references.

    Could anyone tell me what/if is wrong with the following example and what is actually happening behind the scenes with regard to the reference:

    Code:
    bool somefunc(std::complex<long double>& rslt)
    {
      rslt = 0.0L;
    ...
       rslt = x * y;
    
       return true;
    }
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    When you pass something by reference, you pass the address to that object. A nice analogy is

    Code:
    int i;
    i = 10;
    literal(10);
    variable(i);
    Both functions are actually passed the same value in this code, but it's by different methods. I is a variable that holds the value 10, and 10 literally means 10. When you pass a reference by using the address & operator, it's like passing a memory address as a literal. When you use a point, it's a variable that contains that memory address.

  3. #3
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Hi there,

    Thanks for the reply. My question really relates to how do I initialise a value within a function passed by reference?

    If I do the following:

    bool somefunc(std::complex<long double>& rslt)

    How do I initialise the contents of rslt? I.e., if I do this:

    rslt = 10.0L;

    Am I re-initialising (copying) the rslt data to be 10, or am I 'pointing' rslt at the literal 10?

    How exactly does this work?

    If I am simply changing the reference to point somewhere else, how do I prevent this?

    What I want to do is to use 'pass by reference' to return a result. If I were using pointers, I would do the following:

    Code:
    bool somefunc(std::complex<long double>* rslt)
    {
      *rslt = 10.0L;
    ...
    *rslt = x * y;
    return true;
    }
    I have been using references to try and do this without having to dereference the pointer. However, I think it has been giving me problems, and I suspect that what I have been doing is wrong. Can I use references to return in results in this way, or should I stick to passing pointers?
    Last edited by Davros; 10-11-2004 at 03:00 PM.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    umm...i'm not sure i understand the question, but, i'll try to answer anyway...

    funct(int &x)
    {
    x = x + 1;
    return x;
    }

    you are basically doing the same as *x = 1, but without the *

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    The following two functions do the same thing.
    Code:
    bool somefunc1(std::complex<long double>& rslt)
    {
      rslt = 10.0L;
      // ...
      rslt = x * y;
      return true;
    }
    
    bool somefunc2(std::complex<long double>* rslt)
    {
      *rslt = 10.0L;
      // ...
      *rslt = x * y;
      return true;
    }
    
    int main()
    {
      std::complex<long double> var = 5.0L;
      somefunc1(var);
      somefunc2(&var);
    }
    The reference is an alias for the original variable. So your first line of code does not initialize rslt to 10, because the variable that rslt refers to should have already been intialized by the code that called the function (in the example main above, var is initalized to 5, so when inside of somefunc1, rslt refers to var, which is 5). When you assign 10 to rslt, the variable it is an alias for then has that value as well. Same thing for assigning x * y (in the example above, var would receive the result of x*y when somefunc1 ended).

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    void somefunction(std::complex<long double> &);
    int main()
    {
       std::complex<long double> result;
       somefunction(result);
       cout << result;
    }
     
    void somefunction(std::complex<long double> & rslt)
    {
       rslt = 4.125;
    }

  7. #7
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Please bear with me if my question seems confusing.

    >you are basically doing the same as *x = 1, but without the *

    So if I follow you correctly, I can pass by reference, manipulate the value within the function just as if I have declared the value within the function, and the result is there for use by whatever called the function.

    This was my thinking when I originally wrote my code. However, I have a function which does exacly this:

    Code:
    bool somefunc(std::complex<long double>& rslt)
    {
    ...
      rstl = muliply(x, y);
      // rslt = x * y;
    
      return true;
    }
    This works. If I uncomment the commented line, and comment out the other one - then it still works. HOWEVER, if I uncomment out both (the first being superfluous), then a get a runtime floating point error. What's going on?

    This is why I have started to question my use of references. But perhaps I am doing something else wrong.

    Can anyone shed any light?
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  8. #8
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Ahh. I've had a thought, but still need help.

    I simplified my above example. What it really looks like is this:

    Code:
    bool somefunc(const std::complex<long double>& x, const std::complex<long double>& y,
    std::complex<long double>& rslt)
    {
    ...
      rstl = muliply(x, y);
      // rslt = x * y;
    
      return true;
    }
    NOW, when calling it have been doing this:

    Code:
    std::complex y = 10.0L;
    bool ok = somefunc(10.0L, y, y);
    // Here I'm expecting y to 100.0L
    Notice I have passed 'y' into a const input parameter, but I am also using it the same data to return the result.

    Could this be my problem?
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    The code below works fine for me:
    Code:
    #include <iostream>
    #include <complex>
    bool somefunc(const std::complex<long double>& x,
      const std::complex<long double>& y,
      std::complex<long double>& rslt)
    {
        rslt = x * y;
        return true;
    }
    
    int main()
    {
        std::complex<long double> y = 10.0L;
        bool ok = somefunc(10.0L, y, y);
        std::cout << y << std::endl;
    }

  10. #10
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >The code below works fine for me:

    Thanks for that. I just tried the following myself:

    Code:
    bool somefunc(const std::complex<long double>& x,
      const std::complex<long double>& y,
      std::complex<long double>& rslt)
    {
       rslt = x * y;
       rslt = x * y; // Notice appears twice
       return true;
    }
    Calling the following gives me 100:

    std::complex<long double> y = 10.0L;
    somefunc(10.0L, 10.0L, y);

    HOWEVER, the following gives me 1000:

    std::complex<long double> y = 10.0L;
    somefunc(10.0L, y, y);

    Thanks for the help.

    I understand what was happening now. I'm an idiot. I had got used to passing data by const reference, I had started to treat them as 'copies'.
    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

    BigAngryDog.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21