Why pointers? (The answer)

This is a discussion on Why pointers? (The answer) within the C++ Programming forums, part of the General Programming Boards category; Maybe that's why it's so hard...

  1. #46
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,648
    Maybe that's why it's so hard

  2. #47
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Just thought it was ironic...
    Attached Images Attached Images  
    Just Google It. √

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

  3. #48
    Teenage Mutant Ninja Nerd MMD_Lynx's Avatar
    Join Date
    Aug 2004
    Posts
    65
    man...i wanted this post to help eliminate those...why don't these people do at least SOME research before posting...*crowds stares at Lynx with a look that says, "hypocrite..."* uh...
    Stupid people are useful. You can make them do all the mindless tasks you are too lazy to do yourself.

    Sphynx cats are just bald and wrinkly, like old people, and we don't reject them.

  4. #49
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Stroustroup has a point about pointers (pun NOT intended - I don't make such bad puns), but I still prefer using non-const references, because they can't be NULL. I think that's an extremely important point.
    Also, if it's ok to use references if the function name makes clear that the argument will be modified, then you always use references because function names that don't make it clear that the argument will be notified are bad function names. IMHO.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #50
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>but I still prefer using non-const references, because they can't be NULL.
    That's actually one of the points in favour of pointers (echo CornedBee about puns). If you want an optional argument, it's convenient to have a default argument of a NULL pointer rather than have a reference and a set of argument validation flags.
    Just Google It. √

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

  6. #51
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    IF you want an optional argument. Often, I don't, and then I want the security of not dereferencing a NULL pointer without having the runtime cost of checking for one.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #52
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I want the security of not dereferencing a NULL pointer without having the runtime cost of checking for one.
    Good point. And extending that, if arguments aren't optional, it'll help with debugging because passing NULL for a reference will give you a compile-time error (unless you really really try to get around that) and line number rather than a runtime exception/error. But if you DO want optionals, pointers can still be useful.
    Code:
    void somefunc(int& num)
    {
       ++num;
    }
    
    somefunc(*(int*)NULL); //Perhaps that would compile?
    Just Google It. √

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

  8. #53
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It would compile and yield undefined behaviour. But seriously, if someone does this it's his own fault.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #54
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Hence "(unless you really really try to get around that)" And in conclusion, use references mostly, but pointers are OK when they make sense.
    Just Google It. √

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

Page 4 of 4 FirstFirst 1234
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  2. delete and delete[]
    By Hunter2 in forum C++ Programming
    Replies: 13
    Last Post: 06-26-2003, 04:40 AM
  3. Pointers on pointers to pointers please...
    By Morgan in forum C Programming
    Replies: 2
    Last Post: 05-16-2003, 11:24 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM
  5. Pointers pointers pointers....
    By G'n'R in forum C Programming
    Replies: 11
    Last Post: 11-02-2001, 01:02 AM

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