Pointers of reference

This is a discussion on Pointers of reference within the C++ Programming forums, part of the General Programming Boards category; In simple programs with numerous functions, which do you guys prefer to use: 1) References to variables (&) 2) Pointers ...

  1. #1
    Registered User cyberCLoWn's Avatar
    Join Date
    Dec 2003
    Location
    South Africa
    Posts
    124

    Pointers of reference

    In simple programs with numerous functions, which do you guys prefer to use:

    1) References to variables (&)
    2) Pointers (*)

    Just wondering as I read somewhere that you should use references wherever possible.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >which do you guys prefer to use
    If there's a chance that a parameter or return value can be invalid, use a pointer so that you can test for NULL. Otherwise use a reference. Just follow that simple guideline and you shouldn't have problems.
    My best code is written with the delete key.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>or return value can be invalid, use a pointer so that you can test for NULL.

    Or, if you feel like copying Microsoft, you can add an extra parameter that's a reference to a boolean variable, to indicate whether the return value is valid or not. And if you want to make that extra parameter optional, you could change the boolean reference to a pointer instead, which brings you back to the beginning again

    I like references mostly, because dereferencing pointers takes an extra two keystrokes, and doesn't look as neat. Sometimes I like pointers though, like when (as above) I want to make a parameter optional. That can also be a big pain though, because it means you can't just pass a straight value then, you'd need to create a variable.

    i.e.
    int someParameter = 5;
    myFunction(NULL, NULL, parameter, NULL);
    as opposed to
    myFunction(something, something, 5, something);
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    ><snip> to indicate whether the return value is valid or not
    Or you could define a variable that is only used as a sort of poor man's null:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    
    using namespace std;
    
    namespace {
      string null;
    }
    
    string& foo ( string& s )
    {
      if ( rand() % 2 != 0 ) {
        return null;
      }
    
      return s;
    }
    
    int main()
    {
      string s ( "A string!" );
    
      for ( int i = 0; i < 10; i++ ) {
        if ( foo ( s ) == null ) {
          cout<<"No string here!"<<endl;
        }
        else {
          cout<< s <<endl;
        }
      }
    }
    My best code is written with the delete key.

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Does that work? What if foo() returns an empty string anyways?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Does that work?
    Yes.

    >What if foo() returns an empty string anyways?
    Then null should be initialized with a string that couldn't possibly be a valid return of foo. If any string is possible, then you could test the addresses.
    My best code is written with the delete key.

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ah, and here we are back to pointers How DO they get by in pointerless languages? heh...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

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. Reference and Pointers
    By Shal in forum C++ Programming
    Replies: 3
    Last Post: 08-29-2006, 05:48 PM
  4. Strange/false errors
    By Ganoosh in forum Windows Programming
    Replies: 8
    Last Post: 10-20-2005, 05:54 PM
  5. GCC - Strange networking functions error.
    By maththeorylvr in forum Windows Programming
    Replies: 3
    Last Post: 04-05-2005, 01:00 AM

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