Is there any way to empty a const QByteArray?
-
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 onconst QByteArray
? -
@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.
-
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 onconst QByteArray
?@kayakaan02
const_cast<QByteArray&>(myArray).clear();The question is: why do you need to do that ?
-
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.
-
@kayakaan02
const_cast<QByteArray&>(myArray).clear();The question is: why do you need to do that ?
@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.
-
@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.
@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-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.
@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...