Results 1 to 8 of 8

Thread: pointer/reference

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default pointer/reference

    hi, what is better? which different? thanks
    Qt Code:
    1. OBJ o;
    2. OBJ* pointer = o;
    3. this->pointer->....
    4. // OR
    5. OBJ& get();
    6. this->get().
    7. //or
    8. OBJ* get();
    9. this->get()->.....
    To copy to clipboard, switch view to plain text mode 
    Regards

  2. #2
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Thanks
    2
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: pointer/reference

    Basically a reference and a pointer are pretty similar as they both point to a memory segment, the main difference, however, is that references must be initialised to reference a variable and therefore can only be NULL when forced to be null (pointers may not be initialised and be NULL instead).

    Also, your first example is wrong, it must be:
    Qt Code:
    1. OBJ o;
    2. OBJ *pointer = &o;
    To copy to clipboard, switch view to plain text mode 

    &o returns the address of o which must be stored inside the pointer.

  3. #3
    Join Date
    Jul 2006
    Posts
    79
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: pointer/reference

    Qt Code:
    1. OBJ* get();
    2. this->get()->.....
    To copy to clipboard, switch view to plain text mode 
    with this code you have a problem with
    Qt Code:
    1. get()
    To copy to clipboard, switch view to plain text mode 
    then a object would be createt, but you wouldn't be able to access it. this would waste some memory..

    regards..
    aman..

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: pointer/reference

    Use pointers when you have to and references when you can use them.

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: pointer/reference

    Quote Originally Posted by aMan
    Qt Code:
    1. OBJ* get();
    2. this->get()->.....
    To copy to clipboard, switch view to plain text mode 
    with this code you have a problem with
    Qt Code:
    1. get()
    To copy to clipboard, switch view to plain text mode 
    then a object would be createt, but you wouldn't be able to access it. this would waste some memory..
    sorry what's problem with get? it return a pointer to my object; it's an interface to access 'o' for other object... o is a private menber...
    Regards

  6. #6
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: pointer/reference

    Quote Originally Posted by jacek
    Use pointers when you have to and references when you can use them.
    Use? My problem is; how do I have to declare it? which is likely? Before this I know yet I'll must use o; otherwise I don't wonder it..not?
    Regards

  7. #7
    Join Date
    Jul 2006
    Posts
    79
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: pointer/reference

    Quote Originally Posted by mickey
    sorry what's problem with get? it return a pointer to my object; it's an interface to access 'o' for other object... o is a private menber...
    oh, then it's ok..


    @references
    i assume he is thinking of the following:
    Qt Code:
    1. int blub(int& a, int& b);
    To copy to clipboard, switch view to plain text mode 

    see here for details..

    regards..
    aman..

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: pointer/reference

    Quote Originally Posted by mickey
    Use? My problem is; how do I have to declare it? which is likely? Before this I know yet I'll must use o; otherwise I don't wonder it..not?
    In this case use == declare.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.