Initialized reference in function

This is a discussion on Initialized reference in function within the C++ Programming forums, part of the General Programming Boards category; Hey, if I want to have a function that can take a reference value as argument but also have that ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    158

    Initialized reference in function

    Hey, if I want to have a function that can take a reference value as argument but also have that value initialized if no such value is supplied when calling the function, how would that be done?

    I would like something like this (which is not working)

    Code:
    int Function(float &value = 0.0f);
    
    int Function(float &value)
    {
    
    value = 5.0f; return 1;
    }
    So that I can call this function in both these ways:

    Code:
    int vertex = Function();
    Code:
    float value;
    int vertex = Function(value);
    I would rather like to avoid function overloading. Is there a way to write the definition of the function to make it work?
    Last edited by DrSnuggles; 07-22-2008 at 04:24 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can't do that - a reference must always refer to a valid location. Overloading (one function that takes no parameter, and one that takes a float reference) is your only option [you could of course make the first function call the second with a dummy parameter].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    Ok thanks, that makes sense I guess. Function overloading will be my option then.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by DrSnuggles View Post
    Ok thanks, that makes sense I guess. Function overloading will be my option then.
    Of course, the other solution would be to pass a pointer, which can be NULL initialized - but that means that you have to put &value everywhere in your code that calls the function.

    Like this:
    Code:
    int Function(float *value = NULL);
    
    int Function(float &value)
    {
    
       if (value) 
           *value = 5.0f; 
       return 1;
    
    }
    
    int vertex = Function();
    ... 
    float value;
    int vertex = Function(&value);

    Edit: to a large extent, the solution will depend on what the meaning is of the call, and what "no parameter" means in comparison to "with parameter".


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    Aha yes, I recognize that way of doing it. I think that is what I need in this situation. Thanks!

    I want to use this to get the edge on a 3d mesh that the mouse is closest to and in some cases when I call the function I also want to get where on the edge the mouse is (0.0 - 1.0).
    Last edited by DrSnuggles; 07-22-2008 at 05:21 AM.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    Hmm, if I write it the way you did then I get:

    error C2100: illegal indirection

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sorry, missed changing this line:
    Code:
    int Function(float &value)
    // Should be:
    int Function(float *value)
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    Ok, works fine, it's all good.

    Although... I havn't tested yet but...

    Simply defining:

    Code:
    float value;
    and passing that to the funcrtion would not produce a true value in a conditional test within the function right? Wouldn't that be interpreted as 0?
    Last edited by DrSnuggles; 07-22-2008 at 05:52 AM.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by DrSnuggles View Post
    Ok, works fine, it's all good.

    Although... I havn't tested yet but...

    Simply defining:

    Code:
    float value;
    and passing that to the funcrtion would not produce a true value in a conditional test within the function right? Wouldn't that be interpreted as 0?
    But you should be passing &value to the function, and if that's zero, then you either have a very specific embedded system or something has gone horribly wrong. Not very likely, and certainly not the case on Windows, Linux, Solaris or any other major system.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    Right, now I understand. The memory adress of the value can not be zero. All fine then, thanks for the help again.

  11. #11
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Don't be afraid of function overloading. It really is the best way to enable users to use your function in different ways. If not, users might be obliged to casts (static_cast, const_cast), and that's really what you don't want.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MarkZWEERS View Post
    Don't be afraid of function overloading. It really is the best way to enable users to use your function in different ways. If not, users might be obliged to casts (static_cast, const_cast), and that's really what you don't want.
    I do agree with that - and in some cases, like one I worked on a day or two ago, overloading a function to avoid passing some "unused" variable will actually help the performance - I'm not sure exactly how much better the code I improved was, but it was traversing a tree, using a reference to a pointer which was never used in the calling code, but because the reference was there the compiler HAD to update via several indirections. The function went from 5-6th place in the profile to below 15th by removing that parameter, and just using a local variable to traverse the tree. The compiler could now hold the "current point" in a register, and saved several "update the reference" instructions. My guess is that this function is about 10x faster than the old one, and about half the size.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    might as well make it inline too.

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    Quote Originally Posted by MarkZWEERS View Post
    Don't be afraid of function overloading. It really is the best way to enable users to use your function in different ways. If not, users might be obliged to casts (static_cast, const_cast), and that's really what you don't want.
    Yeah I have lots of them in fact, but this perticular function is quite large so it will be easier to manage when changes is required to only have one function.

    Quote Originally Posted by matsp
    I do agree with that - and in some cases, like one I worked on a day or two ago, overloading a function to avoid passing some "unused" variable will actually help the performance - I'm not sure exactly how much better the code I improved was, but it was traversing a tree, using a reference to a pointer which was never used in the calling code, but because the reference was there the compiler HAD to update via several indirections. The function went from 5-6th place in the profile to below 15th by removing that parameter, and just using a local variable to traverse the tree. The compiler could now hold the "current point" in a register, and saved several "update the reference" instructions. My guess is that this function is about 10x faster than the old one, and about half the size.
    Cool, something to consider.

    Quote Originally Posted by robwhit
    might as well make it inline too.
    Well the actual function is a lot larger than that. What I posted was a simple example. Like I said I need to use this to get the closest edge on a 3d mesh and I would expect a function that always returns a float value of 5.0 to produce any miracles.

  15. #15
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,686
    Why wouldn't this work?
    Code:
    int Function(float &value)
    {
       value = 5.0f;
       return 1;
    }
    
    int Function()
    {
       float value = 0.0;
       return Function(value);
    }
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Textbox
    By maxorator in forum Windows Programming
    Replies: 20
    Last Post: 09-25-2005, 11:04 AM

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