Results 1 to 5 of 5

Thread: what's the difference between &conn and *conn

  1. #1
    Join Date
    Jun 2010
    Posts
    32
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    8

    Question what's the difference between &conn and *conn

    Hi everyone,
    I was trying to add a class object pointer to a QList<A*> list.

    Qt Code:
    1. Class A
    2. {
    3. QString myName;
    4. public QString Desc()
    5. {
    6. return myName;
    7. }
    8.  
    9. }
    10.  
    11. Class B
    12. {
    13. QList<A*> list;
    14. public void Add(QString s){
    15. A myOb;
    16. list.append(&myOb);
    17. }
    18. public void search(QString s)
    19. {
    20. for(int i=0;i<list.size();++i){
    21. if (list[i].Desc() ==s) {
    22.  
    23. }
    24. }
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

    I get segmentation fault I think.


    Qt Code:
    1. Class A
    2. {
    3. QString myName;
    4. public QString Desc()
    5. {
    6. return myName;
    7. }
    8.  
    9. }
    10.  
    11. Class B
    12. {
    13. QList<A*> list;
    14. public void Add(QString s){
    15. A *myOb=new A();
    16. list.append(myOb);
    17. }
    18. public void search(QString s)
    19. {
    20. for(int i=0;i<list.size();++i){
    21. if (list[i].Desc() ==s) {
    22.  
    23. }
    24. }
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

    It works fine.

    What's the differnce between list.append(&myOb) and list.append(myOb) in above code.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    Wiki edits
    20

    Default Re: what's the difference between &conn and *conn

    Quote Originally Posted by yyiu002 View Post
    Qt Code:
    1. Class A
    2. {
    3. QString myName;
    4. public QString Desc()
    5. {
    6. return myName;
    7. }
    8.  
    9. }
    10.  
    11. Class B
    12. {
    13. QList<A*> list;
    14. public void Add(QString s){
    15. A myOb;
    16. list.append(&myOb);
    17. }
    18. public void search(QString s)
    19. {
    20. for(int i=0;i<list.size();++i){
    21. if (list[i].Desc() ==s) {
    22.  
    23. }
    24. }
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

    I get segmentation fault I think.
    A couple of things you should take into consideration:
    1. You do not have a constructor
    2. Everything is private by default
    3. Try to put the implementation into a cpp file

    Programmatically, there's no difference between using &myObj in your first example and myObj in your second example. Both point to an address.
    However, there's a world of difference when it comes to where the address is defined and how long it lives.

    In short, in the code above you get a segmentation fault because myObj doesn't exist anymore when you want to access it in your search function.
    Your object myObj is a local object defined inside the scope of the add function. It stops existing after you leave the add function, even if you have added to the list.
    Thus, there's nothing wrong with you adding the object to the list. It only doesn't exist anymore a fraction of a second later.

    The second example works because the pointer isn't destroyed when you leave the scope of the add function. However, if you didn't use the list to append the object, it would have really gone out of scope, as in you don't have any control anymore on the object. But in your example you've added it to a list that is available in class B, so that's no problem.

  3. The following user says thank you to tbscope for this useful post:

    yyiu002 (23rd June 2010)

  4. #3
    Join Date
    Jun 2010
    Posts
    32
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    8

    Default Re: what's the difference between &conn and *conn

    I still didn't get it? They both are pointers, but why just first usage of poiner was destroyed?

  5. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: what's the difference between &conn and *conn

    Qt Code:
    1. public: void Add(QString s){
    2. A myOb; // here the myObj will "live" only in this function
    3. list.append(&myOb);
    4. } //here that object is deleted
    5. // here you don't have it any more
    6. // the myObj scope is in that function,
    7. // and you will try to access it later with the pointer from the list (but the object is gone)
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. public: void Add(QString s){
    2. A *myOb=new A(); //here you allocate memory on the heap, this will "live" until delete is called
    3. list.append(myOb);
    4. }
    5. // the object is still valid after the execution of the function
    6. // only the pointer "life" is ended, but you have the pointer from the list that will take care of the actual object
    To copy to clipboard, switch view to plain text mode 

    References are not pointers. DO NOT code like they can be changed, they are two different types of indirect access to an object

  6. The following user says thank you to Zlatomir for this useful post:

    yyiu002 (23rd June 2010)

  7. #5
    Join Date
    Jun 2010
    Posts
    32
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    8

    Default Re: what's the difference between &conn and *conn

    Many thanks to all of you.

Similar Threads

  1. difference between two objects
    By rk0747 in forum Newbie
    Replies: 1
    Last Post: 15th April 2010, 09:30
  2. SigSlot conn from base classs to subclass
    By stevey in forum Qt Programming
    Replies: 2
    Last Post: 5th September 2009, 11:12
  3. Difference between the regulare MVC
    By Jonas_ in forum Newbie
    Replies: 3
    Last Post: 2nd September 2009, 14:59
  4. difference b/w QLineEdit() and QTextEdit()
    By wagmare in forum Qt Programming
    Replies: 7
    Last Post: 20th July 2009, 05:37
  5. Replies: 1
    Last Post: 7th October 2008, 12:11

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
  •  
Qt is a trademark of The Qt Company.