Should I use -> or . ?

This is a discussion on Should I use -> or . ? within the C++ Programming forums, part of the General Programming Boards category; null reference isn't the same as address 0. A null reference would be a reference that doesn't reference anything. It ...

  1. #31
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    null reference isn't the same as address 0. A null reference would be a reference that doesn't reference anything. It logically doesn't make any sense. Even if it referenced nothing it would still be referencing something (the nothingness).

    In the example it did reference something, that something just happened to be invalid.

  2. #32
    The larch
    Join Date
    May 2006
    Posts
    3,573
    But then a pointer also always points to something (whether it is nothing or invalid) and in this regard there isn't any difference?

    In a correct program a pointer can point to nothing but a reference cannot refer to nothing.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #33
    Apprentice Swordsman's Avatar
    Join Date
    Apr 2007
    Posts
    38
    So, when exactly would one use the

    Creature creature;
    Creature creature = new Creature();
    Creature* creature

    initialisations?

    Can anyone think of any use cases to split up how you would use them please?

  4. #34
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    Quote Originally Posted by Swordsman
    Can anyone think of any use cases to split up how you would use them please?
    Generally, I would prefer to write:
    Code:
    Creature creature;
    The idea being that the Creature class (it is a class, right?) would manage its own memory, so I need not worry about that.

    This one is unusual:
    Code:
    Creature creature = new Creature;
    Unless you really intend for Creature to have a constructor that takes a Creature*, you probably wanted to write:
    Code:
    Creature* creature = new Creature;
    I would try not to use this, but instead use a smart pointer such as std::auto_ptr or std::tr1::shared_ptr, e.g.,
    Code:
    std::auto_ptr<Creature> creature(new Creature);
    but note note std::auto_ptr will be deprecated in the next version of C++ in favour of std::unique_ptr, which is not part of the C++ standard library at this point of time. One "use case" that comes to mind would be when you want the lifetime of the object to be beyond the scope in which it was created.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #35
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Quote Originally Posted by laserlight View Post
    I would try not to use this, but instead use a smart pointer such as std::auto_ptr or std::tr1::shared_ptr, e.g.,
    Also auto_ptr doen't support proper deallocation on arrays. For that you need boost::scoped_ptr or something similar. Also be careful about multiple references with auto_ptrs, but they are preferred for RAII.

  6. #36
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    Quote Originally Posted by linuxdude
    Also auto_ptr doen't support proper deallocation on arrays. For that you need boost::scoped_ptr or something similar.
    No, for that you should use a container class, e.g., std::vector. boost::scoped_ptr would have the same "problem" as std::auto_ptr in that respect.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #37
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Quote Originally Posted by laserlight View Post
    No, for that you should use a container class, e.g., std::vector. boost::scoped_ptr would have the same "problem" as std::auto_ptr in that respect.
    Sorry, I meant boost::scoped_array.

  8. #38
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    BTW, does the standard guarantee that doing this trick (int &ref = *(int *)NULL) crashes immediately? Else you could do

    Code:
    #include <iostream>
    
    template <typename Type>
    void swap_by_pointers(Type *a, Type *b)
    {
        if (a != NULL && b != NULL) {
            Type temp = *a;
            *a = *b;
            *b = temp;
        }
    }
    
    template <typename Type>
    void swap(Type &a, Type &b)
    {
         swap_by_pointers(&a, &b);
    }
    
    int main(void)
    {
        int four = 4;
        int &ref = *(int *)NULL;
        swap(four, ref);
        std::cout << "four is " << four << ". (Can't show ref)" << std::endl;
    }

  9. #39
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    BTW, does the standard guarantee that doing this trick (int &ref = *(int *)NULL) crashes immediately?
    I think we already said that it's undefined behavior. Look it up.
    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

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. small -> big -> bigger -> bigger than bigger -> ?
    By happyclown in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 03-11-2009, 12:12 PM
  2. Dev-C++ -> Tools - > Editor -> Syntax
    By Yuri2 in forum C++ Programming
    Replies: 19
    Last Post: 07-03-2006, 07:48 AM
  3. > > > Urgent Help < < <
    By CodeCypher in forum C Programming
    Replies: 2
    Last Post: 01-31-2006, 01:06 PM
  4. electricity > AC circuits > tesla coil
    By dbaryl in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 03-14-2002, 01:16 PM

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