Results 1 to 7 of 7

Thread: Object or pointer to object?

  1. #1
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Object or pointer to object?

    Hi there. To make it clear, I'm no OOP, C++ or Qt expert, so I made a few simple functions to clear up a few things about objects and pointer to objects. Here is the code, a class named cat:
    Qt Code:
    1. class Cat {
    2. public:
    3. void SetColor(string clr) {
    4. color = clr;
    5. }
    6.  
    7. string GetColor() {
    8. return color;
    9. }
    10.  
    11. private:
    12. string color;
    13. };
    To copy to clipboard, switch view to plain text mode 
    , and sample usage:
    Qt Code:
    1. void MainWindow::GenerateCats() {
    2. Cat *tofi = new Cat();
    3. tofi->SetColor("orange");
    4. printf("tofi color1 :%s\n",tofi->GetColor().c_str());
    5.  
    6. Cat smuki;
    7. printf("smuki color 1:%s\n",smuki.GetColor().c_str());
    8. smuki.SetColor("black");
    9. printf("smuki color 2:%s\n",smuki.GetColor().c_str());
    10.  
    11. Cat *sivkolin = &smuki;
    12. printf("sivkolin color 1:%s\n", sivkolin->GetColor().c_str());
    13.  
    14. smuki.SetColor("gray");
    15. printf("sivkolin color 2:%s\n", sivkolin->GetColor().c_str());
    16.  
    17. ProcessCats1("tofi",*tofi,"white");
    18. printf("tofi color 2 :%s\n", tofi->GetColor().c_str());
    19.  
    20. ProcessCats1("smuki",smuki,"white");
    21. printf("smuki color 3:%s\n", smuki.GetColor().c_str());
    22.  
    23. ProcessCats2("smuki",&smuki,"no color");
    24. printf("smuki color 4:%s\n", smuki.GetColor().c_str());
    25.  
    26. printf("sivkolin color 3:%s\n", sivkolin->GetColor().c_str());
    27.  
    28. ProcessCats2("sivkolin",sivkolin,"gold");
    29. printf("sivkolin color 4:%s\n", sivkolin->GetColor().c_str());
    30.  
    31. printf("smuki color 5:%s\n", smuki.GetColor().c_str());
    32.  
    33. delete tofi;
    34. //delete smuki;
    35. //smuki = NULL;
    36. delete sivkolin;
    37. }
    38.  
    39. void MainWindow::ProcessCats1(string name,Cat CatSample,string clr) {
    40. printf("ProcessCat1 Cat %s color:%s\n",name.c_str(),CatSample.GetColor().c_str());
    41. CatSample.SetColor(clr);
    42. }
    43.  
    44. void MainWindow::ProcessCats2(string name,Cat *CatSample,string clr) {
    45. printf("ProcessCats2 Cat %s color:%s\n",name.c_str(), CatSample->GetColor().c_str());
    46. CatSample->SetColor(clr);
    47. }
    To copy to clipboard, switch view to plain text mode 
    the output looks like:
    Qt Code:
    1. tofi color1 :orange
    2. smuki color 1:
    3. smuki color 2:black
    4. sivkolin color 1:black
    5. sivkolin color 2:gray
    6. ProcessCat1 Cat tofi color:orange
    7. tofi color 2 :orange
    8. ProcessCat1 Cat smuki color:gray
    9. smuki color 3:gray
    10. ProcessCats2 Cat smuki color:gray
    11. smuki color 4:no color
    12. sivkolin color 3:no color
    13. ProcessCats2 Cat sivkolin color:no color
    14. sivkolin color 4:gold
    15. smuki color 5:gold
    To copy to clipboard, switch view to plain text mode 
    My questions:
    1. Is "->" notation when accessing members addressed with a pointer a C++ or Qt specific?
    2. Function ProcessCats1 expects an object of type Cat not a pointer, why must I use
      Qt Code:
      1. ProcessCats1("tofi",*tofi,"white");
      To copy to clipboard, switch view to plain text mode 
      instead of
      Qt Code:
      1. ProcessCats1("tofi",&tofi,"white");
      To copy to clipboard, switch view to plain text mode 
    3. Same analogy with function ProcessCats2 which requires a pointer to object Cat and I do
      Qt Code:
      1. ProcessCats2("smuki",&smuki,"no color");
      To copy to clipboard, switch view to plain text mode 
      , but smuki is an object, why do I need the "&" meaning "the value at address".
    4. I can delete object created on the heap like this
      Qt Code:
      1. delete tofi;
      To copy to clipboard, switch view to plain text mode 
      , but how do I delete smuki in the scope of same function if I need (I know it gets destroyed when out of scope).
    5. When I do
      Qt Code:
      1. delete sivkolin
      To copy to clipboard, switch view to plain text mode 
      I delete just the pointer to value of smuki, so I can still access object smuki right?
    Much thanks if advance for your help.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Object or pointer to object?

    Quote Originally Posted by arcull View Post
    Is "->" notation when accessing members addressed with a pointer a C++ or Qt specific?
    C/C++

    Function ProcessCats1 expects an object of type Cat not a pointer, why must I use
    Qt Code:
    1. ProcessCats1("tofi",*tofi,"white");
    To copy to clipboard, switch view to plain text mode 
    instead of
    Qt Code:
    1. ProcessCats1("tofi",&tofi,"white");
    To copy to clipboard, switch view to plain text mode 
    Because * is a dereference operator that returns an object under a given pointer and operator & is a reference operator that returns a pointer to a given object.

    http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B


    Same analogy with function ProcessCats2 which requires a pointer to object Cat and I do
    Qt Code:
    1. ProcessCats2("smuki",&smuki,"no color");
    To copy to clipboard, switch view to plain text mode 
    , but smuki is an object, why do I need the "&" meaning "the value at address".
    See above.

    I can delete object created on the heap like this
    Qt Code:
    1. delete tofi;
    To copy to clipboard, switch view to plain text mode 
    , but how do I delete smuki in the scope of same function if I need (I know it gets destroyed when out of scope).
    It's a matter of understanding heap-allocated objects and stack-allocated objects rather than scopes. Every local variable is destroyed when it goes out of scope by unwinding the stack. Heap-based variables (allocated using new operator) are never destroyed automatically.

    When I do
    Qt Code:
    1. delete sivkolin
    To copy to clipboard, switch view to plain text mode 
    I delete just the pointer to value of smuki
    No, delete takes a pointer to the object that is to be deleted. So it is the object that gets deleted and not the pointer. The pointer will be freed when the scope of its declaration ends.

    , so I can still access object smuki right?
    No, but you can still access the pointer
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Object or pointer to object?

    thanks for explanation wysota, but I still don't know how to delete object created on the stack, which is destroyed as soon as the function gets out of scope, but let's I want to delete it inside the same function. Remember I have
    Qt Code:
    1. Cat *tofi = new Cat();
    2. Cat smuki;
    3.  
    4. delete tofi; //object on the heap deleted ok
    5. delete smuki; // I can't do this
    6. smuki = NULL; //neither this
    To copy to clipboard, switch view to plain text mode 
    Thanks again.

  4. #4
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Object or pointer to object?

    It will be deleted automatically when it goes out of scope.

  5. #5
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Object or pointer to object?

    Quote Originally Posted by stampede View Post
    It will be deleted automatically when it goes out of scope.
    Thanks I know that, but can it be deleted while still in scope. It is not that I really need this, but would just like to make clear what can and what can't be done, thanks.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Object or pointer to object?

    No, it cannot. The definition of a local variable is that its life is determined by the scope of its declaration. But you can limit the scope if you want.

    Qt Code:
    1. Cat *tofi = new Cat();
    2. {
    3. Cat smuki;
    4.  
    5. delete tofi; //object on the heap deleted ok
    6. } // smuki undeclared past this point
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Oct 2013
    Posts
    102
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Object or pointer to object?

    No, it cannot. The definition of a local variable is that its life is determined by the scope of its declaration. But you can limit the scope if you want.

    Qt Code:
    Switch view

    Cat *tofi = new Cat();
    {
    Cat smuki;

    delete tofi; //object on the heap deleted ok
    } // smuki undeclared past this point
    Very good, thanks.

Similar Threads

  1. TypeError: Object [object Object] has no method 'sendData'
    By TheIndependentAquarius in forum Qt Quick
    Replies: 2
    Last Post: 30th November 2013, 05:54
  2. Help with Q_PROPERTY with object pointer
    By lni in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2009, 17:31
  3. static Object Vs Pointer
    By rajeshs in forum General Programming
    Replies: 4
    Last Post: 11th June 2008, 07:41
  4. Saving object pointer
    By MarkoSan in forum General Programming
    Replies: 4
    Last Post: 11th January 2008, 11:53
  5. Pointer to ActiveX object in QT
    By json84 in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2007, 12:42

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.