Reference or Pointer

This is a discussion on Reference or Pointer within the C++ Programming forums, part of the General Programming Boards category; Would someone be able to tell me which of these methods of passing data is this best? Thanks. Code: #include ...

  1. #1
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359

    Reference or Pointer

    Would someone be able to tell me which of these methods of passing data is this best? Thanks.

    Code:
    #include <iostream.h>
    
    const int MAX_TYPES=2, MAX_LEN=10;
    
    void Test(int *, const char [MAX_TYPES][MAX_LEN], const int);
    
    int main(void)
    {
      const char TYPE[MAX_TYPES][MAX_LEN]={{"Reference"},{"Pointer"}};
      const int REF=0, POINT=1;
      int Num, *NumPointer=&Num;
    
      cout << "Enter number: ";
      cin >> Num;
    
      Test(&Num, TYPE, REF);
      Test(NumPointer, TYPE, POINT);
    
      return 0;
    }
    
    void Test(int *Test, const char Type[MAX_TYPES][MAX_LEN], const int TypeCat)
    {
      cout << "\nAddress of test: " << Test << ".\nValue of test: " << *Test
           << " (passing by " << Type[TypeCat] << ").\n";
      cin.get();
    }
    Be a leader and not a follower.

  2. #2
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161

    Lightbulb

    C++ programmers strongly prefer references to pointers. References are cleaner and easier to use, and they do a better job of hiding information.
    References cannot be reassigned, however. If you need to point first to one object and then another, you must use a pointer. References cannot be null, so if there is any chance that the object in question may be null, you must not use a reference. You must use a pointer


    this was borrowed from "teach yourself c++ in 21 days"

    i hope its clear for u
    Programming is a high logical enjoyable art for both programer and user !!

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    273
    moemen ahmed's post says it all I think. Personally, I used to use references quite a lot but I work with old-C coders and they are scared of certain C++ features including references so I figure that pointers are just fine.

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    151

    Exclamation Stop

    Neither of your examples use the C++ reference syntax. They both pass the address of a variable; therefore pass a pointer. Your function only takes a pointer as a parameter; so it wouldn't be possible to pass a reference anyway.

    In regard to which is best it'd probably be the first as the second method involves the creation of an unnecessary variable.

  5. #5
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Thanks for your replies. I think it's a matter of choice rather than requirement whether to use one or the other, in most cases.
    Be a leader and not a follower.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by subdene
    Thanks for your replies. I think it's a matter of choice rather than requirement whether to use one or the other, in most cases.
    It's a choice in C++ , in C you do not have a choice you must use pointers.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Using Vectors. MinGW warning
    By Viewer in forum C++ Programming
    Replies: 9
    Last Post: 03-26-2009, 03:15 PM
  3. Strange/false errors
    By Ganoosh in forum Windows Programming
    Replies: 8
    Last Post: 10-20-2005, 04:54 PM
  4. Textbox
    By maxorator in forum Windows Programming
    Replies: 20
    Last Post: 09-25-2005, 10:04 AM
  5. C++ pointer vs reference
    By C-Dumbie in forum C++ Programming
    Replies: 4
    Last Post: 11-04-2002, 03:08 AM

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