Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. [Solved] creating new Object in const method
Forum Updated to NodeBB v4.3 + New Features

[Solved] creating new Object in const method

Scheduled Pinned Locked Moved C++ Gurus
8 Posts 4 Posters 3.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Asperamanca
    wrote on last edited by
    #1

    Ok, I admit, from the title alone, it sounds like "oh no!". But there's good reason for my madness.

    The problem in a nutshell:

    Within a 'const' method, I create a new Object:

    @m_pLayoutTextDocument = new QTextDocument(this);@

    which has been declared as 'mutable'

    @mutable QTextDocument* m_pLayoutTextDocument;@

    Even though declared as mutable, gcc complains:
    @error: invalid conversion from 'const QObject*' to 'QObject*'@

    The reason why I am doing this at all is lazy initialization, a pattern I use regularly:
    The m_pLayoutTextDocument I create will, for many instances of my class, not be used at all. Since my object can be created often, and only some of them us the TextDocument (for calculation of preferred size, btw.), I want to delay the creation of this object until the last possible time.

    The last possible time (and a very logical one, too) is when the user of the class calls
    @myclass.getPreferredSize();@
    which is a 'const' method (otherwise, the user of my class gets into trouble with his const correctness).

    So, within getPreferredSize(), I have
    @
    if ( ! m_bPreferredSizeValid )
    {
    updatePreferredSize();
    }

    return m_LastPreferredSize;
    

    @

    so updatePreferredSize() must be 'const' too, and hence the problem.

    One possible solution would be to cast away my own const-ness in order to call a non-const method creating the object.

    Do you think that it would be safe (while undoubtedly ugly) to do the following:

    @
    CTpeb_GraphicsTextPrivate* thisMutable = const_cast<CTpeb_GraphicsTextPrivate*>(this);
    // Now I can call a non-const method which creates my object
    thisMutable->createLayoutDocument();
    @

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tobias.hunger
      wrote on last edited by
      #2

      The constructor of QTextDocument takes a QObject * as parent. You are passing "this" there, which is const in a a const method. So you could pass no parent to get around the problem, but then you will need to make sure the m_pLayoutTextDocument is going to be deleted yourself.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Asperamanca
        wrote on last edited by
        #3

        Hmmmm....since the object is private to my class and ownership is never going to move around, deleting it in my destructor should be pretty risk-free.

        Thanks, that sounds like a better solution.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You can also use a "QScopedPointer":http://qt-project.org/doc/qt-4.8/qscopedpointer.html for that purpose

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Asperamanca
            wrote on last edited by
            #5

            Even better, thanks! Seems like I'm still asleep after a long weekend. ;-)

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You're welcome !

              Don't forget to update the thread's title to solved so other forum users may know that a solution has been found

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Asperamanca
                wrote on last edited by
                #7

                Right. I always forget that. I think a "Solved my issue" button next to a reply post would be great for that.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  Yeah, the Q&A feature that has been on test for so long by now might be actually used...

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved