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; Originally Posted by Yarin Why wouldn't this work? Code: int Function(float &value) { value = 5.0f; return 1; } int ...

  1. #16
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    Quote Originally Posted by Yarin View Post
    Why wouldn't this work?
    Code:
    int Function(float &value)
    {
       value = 5.0f;
       return 1;
    }
    
    int Function()
    {
       float value = 0.0;
       return Function(value);
    }
    Yes that would work but it would mean I would have to define "float value" everytime I call the function even if I don't use it. Most of the time I will not be using that value so I would rather like to have the option to call the function without arguments.

    Edit: I misinterpreted you code... might be more clever than I thought... so you mean I have all the code in the "int Function(float &value)" function and just wraps that function with an overloaded function without arguments?
    Last edited by DrSnuggles; 07-24-2008 at 01:59 PM.

  2. #17
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by DrSnuggles View Post
    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.
    No no, implement one in terms of the other. Like this...
    Quote Originally Posted by DrSnuggles View Post
    Edit: I misinterpreted you code... might be more clever than I thought... so you mean I have all the code in the "int Function(float &value)" function and just wraps that function with an overloaded function without arguments?
    And if you have the wrapper function inline, there will be only one function call instead of two.

  3. #18
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Of course, almost any optimising compiler would probably figure out how to inline a one-line function by itself. It's probably still a good idea to use inline, however, if only for the reader.

    That's assuming that the pointer method didn't work out . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #19
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Maybe you don't have an optimizing compiler.

  5. #20
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Maybe you should get an optimising compiler.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #21
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Maybe your platform doesn't have an optimizing compiler.

  7. #22
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I'm pretty sure that Windows does. Getting the main hardrive?

    But I know what you mean. I'd probably put inline in myself, I was just pointing out that optimising compilers likely make the keyword redundant. (Remember register? . . . .)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #23
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Truce.


Page 2 of 2 FirstFirst 12
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, 06:54 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. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  5. Textbox
    By maxorator in forum Windows Programming
    Replies: 20
    Last Post: 09-25-2005, 10:04 AM

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