Solved Type casting in Qt
-
Hi all,
What is a better way to do typecasting in Qt
I'm getting warning saying "use of old-style cast
" by typecasting following way:function((uint8_t*)data.data(), (size_t)data.length());
What is the better way to do this?
Thanks in advance.
-
@viniltc
You're "not allowed" to use those (lovely) C-style casts any longer. This isn't really Qt, it's C++. You should Google for genericstatic_cast<>
&dynamic_cast<>
for use in C++ code (e.g. https://en.cppreference.com/w/cpp/language/static_cast), and forqobject_cast<>
in the Qt docs for a specific one useful in some Qt circumstances.I believe the compiler will be happier with your code if you try:
function(static_cast<uint8_t*>(data.data()), static_cast<size_t>(data.length()));
or probably better If it's OK with the type coercing:
function(reinterpret_cast<uint8_t*>(data.data()), reinterpret_cast<size_t>(data.length()));
-
@JonB
Thanks a lot for the feedback :) -
Hi @viniltc,
so is your problem solved? Then pleas mark this topic as SOLVED too. Thanks!