Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is there any way to empty a const QByteArray?

Is there any way to empty a const QByteArray?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 6 Posters 733 Views
  • 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.
  • K Offline
    K Offline
    kayakaan02
    wrote on last edited by
    #1

    I want to empty a const QByteArray. If possible also want to make it null. How can I do that since .clear() doesn't work on const QByteArray?

    M 1 Reply Last reply
    0
    • JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by
      #2

      const means you can not change it. You have make a copy of it and clear the copy.

      SGaistS 1 Reply Last reply
      0
      • JoeCFDJ JoeCFD

        const means you can not change it. You have make a copy of it and clear the copy.

        SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @JoeCFD In that case, it would be simpler to use a new QByteArray object. Copying and clearing is just a waste of time and resources.

        @kayakaan02 Can you explain your use case ? As @JoeCFD wrote, you are trying to change an object that you have defined as constant.

        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
        1
        • K kayakaan02

          I want to empty a const QByteArray. If possible also want to make it null. How can I do that since .clear() doesn't work on const QByteArray?

          M Offline
          M Offline
          mpergand
          wrote on last edited by
          #4

          @kayakaan02
          const_cast<QByteArray&>(myArray).clear();

          The question is: why do you need to do that ?

          Christian EhrlicherC 1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Well const specifically means it's immutable, so it already suggest you shouldn't do that.

            There is a way in the language to make it mutable via const_cast, but that's a very specific tool that shouldn't be used lightly. It's very easy to invoke undefined behavior with it, so it may or may not let you do what you want, depending on what's the storage situation of your variable. See const_cast conversion for more detail.

            In any case, if you ended up having to modify a const variable there's rather something wrong with your design, so I'd look at that first.

            1 Reply Last reply
            2
            • M mpergand

              @kayakaan02
              const_cast<QByteArray&>(myArray).clear();

              The question is: why do you need to do that ?

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by Christian Ehrlicher
              #6

              @mpergand said in Is there any way to empty a const QByteArray?:

              const_cast<QByteArray&>(myArray).clear();

              Please stop with such ideas here. First it will break the c++ rules (and can also lead to undefined behavior) and second based on the question it's clear that there is a lack of basic understanding so suggesting such hacks will hurt more than it helps.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              M 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                @mpergand said in Is there any way to empty a const QByteArray?:

                const_cast<QByteArray&>(myArray).clear();

                Please stop with such ideas here. First it will break the c++ rules (and can also lead to undefined behavior) and second based on the question it's clear that there is a lack of basic understanding so suggesting such hacks will hurt more than it helps.

                M Offline
                M Offline
                mpergand
                wrote on last edited by
                #7

                @Christian-Ehrlicher said in Is there any way to empty a const QByteArray?:

                Please stop with such ideas here.

                Take it easy man :)

                such hacks will hurt more than it helps.

                Hence my question in my post above.

                Christian EhrlicherC 1 Reply Last reply
                0
                • M mpergand

                  @Christian-Ehrlicher said in Is there any way to empty a const QByteArray?:

                  Please stop with such ideas here.

                  Take it easy man :)

                  such hacks will hurt more than it helps.

                  Hence my question in my post above.

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @mpergand said in Is there any way to empty a const QByteArray?:

                  Please stop with such ideas here.

                  Take it easy man :)

                  There are enought people around which simply use such stuff without thinking which result in more such strange questions as above...

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  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