Results 1 to 9 of 9

Thread: Basic C++ doubt

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Posts
    667
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    10
    Thanked 80 Times in 74 Posts

    Default Re: Basic C++ doubt

    It might be a very silly doubt, but please bear with me.

    Now let us consider the following code.

    Qt Code:
    1. class Fred {
    2. public:
    3. static FredPtr create();
    4. static FredPtr create(int i, int j);
    5. ...
    6. private:
    7. Fred();
    8. Fred(int i, int j);
    9. ...
    10. };
    11.  
    12. class FredPtr { /* ... */ };//the code above.
    13.  
    14. inline FredPtr Fred::create() { return new Fred(); }//Isn't the return type wrong ?
    15. inline FredPtr Fred::create(int i, int j) { return new Fred(i,j); }
    To copy to clipboard, switch view to plain text mode 

    and the code here

    Qt Code:
    1. FredPtr ptr1(Fred::create());
    2. FredPtr ptr2 = ptr1;//What will happen here ?
    To copy to clipboard, switch view to plain text mode 

    Can you please explain how count will change in this case ?

    Another doubt

    Fred* const old = p_; means old is a const pointer to a const Fred. Then how can it be changed in the statement "if (--old->count_ == 0)" ?

    Thanks a lot.
    Last edited by munna; 30th November 2006 at 15:33. Reason: updated contents

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

    Default Re: Basic C++ doubt

    Quote Originally Posted by munna View Post
    inline FredPtr Fred::create() { return new Fred(); }//Isn't the return type wrong ?
    No, because there's FredPtr::FredPtr(Fred* p) constructor[1] that will be used to convert Fred* to FredPtr.

    Quote Originally Posted by munna View Post
    FredPtr ptr1(Fred::create());
    FredPtr ptr2 = ptr1;//What will happen here ?
    Both FredPtrs will point to the same Fred object and the reference count will be 2.

    Quote Originally Posted by munna View Post
    Fred* const old = p_; means old is a const pointer to a const Fred.
    No, it's a constant pointer to a non-constant object.

    [1] and it doesn't have "explicit" keyword in front of it.

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

    Default Re: Basic C++ doubt

    I think it's also worth to note that in this situation:
    Qt Code:
    1. FredPtr ptr2 = ptr1;
    To copy to clipboard, switch view to plain text mode 
    the compiler will use a copy constructor (line 20 of the listing in the first post) and not the assignment operator (lines 21-32 of the same listing).

Similar Threads

  1. Basic Qt4 tutorial needed
    By pthomas in forum Qt Programming
    Replies: 26
    Last Post: 16th October 2006, 16:11
  2. Replies: 5
    Last Post: 4th August 2006, 11:12
  3. Basic C++ doubts
    By munna in forum General Programming
    Replies: 3
    Last Post: 6th April 2006, 17:23
  4. Need Basic html Browser
    By awalesminfo in forum Newbie
    Replies: 6
    Last Post: 21st March 2006, 18:14
  5. Basic question on new and delete
    By jcr in forum General Programming
    Replies: 25
    Last Post: 14th February 2006, 16:09

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.