-1
#include <QDebug>
#include <QString>

int main()
{   
    char aaa[] = {"abcdefg"};
    QString bbb(QString::fromLatin1(aaa));
    qDebug()<<bbb;
    qDebug()<<((QString)aaa); //dangerous way
    qDebug()<<(reinterpret_cast<QString>(aaa)); //dangerous way

    return a.exec();
}

As long As I know, this may cause some undefined behavior, yet the codes work How could the c-style cast work?

  • os : win7 64
  • compiler : mingw4.8.2
  • Qt : 5.2

edit : Is this a safe operation?

user3518786
  • 17
  • 1
  • 6
  • What is your question? – vahancho May 28 '14 at 09:31
  • 1
    http://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-const-cast-and-reinterpret-cast-be-used?rq=1 – Mat May 28 '14 at 09:35
  • It works because Qt defines this cast and it's being used a lot as implicit cast for example in `someStuff.setText ("abcdefghj")`. If you would cast it to pointer to `QString` though that would be truly dangerous and wouldn't work ofc. – Predelnik May 28 '14 at 09:56

3 Answers3

2

qt defines conversion from char* to QString using a constructor

this means that the c-style cast ((QString)aaa) is well defined (through the static_cast mechanism which tries constructors) though dangerous in terms of encodings.

You can disable this by defining QT_NO_CAST_FROM_ASCII

ratchet freak
  • 47,288
  • 5
  • 68
  • 106
0

Your question is : Is there a satefy way to cast char* into QString ? Because this question already has an answer ...

Qt char* to QString

Kind regards

Community
  • 1
  • 1
Wiist
  • 25
  • 8
  • I know the safe way, what I want to know is how could c style cast work? – user3518786 May 28 '14 at 09:38
  • @user3518786 by sheer luck. – eerorika May 28 '14 at 09:41
  • C style casts should be avoided in C++. I never encountered situation when it was needed except laziness (it's shorter to write then static_cast, const_cast, etc.). Why do you need it? –  May 28 '14 at 09:46
0

"Is this a safe operation?"

No. C-style casting in C++ is not generally considered as safe operation. Reason? There is a lots of different kinds of casts and using C-style cast means you tell the compiler that it should select one of them. Problem is that this might lead to different result than you wanted. One chapter in "C++ Coding Standards : Rules, Guidelines, and Best Practices" is about that.


Example imagine this:

class Based { ... }

class Derived : Based { ... }

and now source file #1:

class Based;
class Derived;

Derived* boo();

Based* foo1()
{
    Derived *a = boo();
    Based *b = (Based*)a;
    return b;
}

source #2:

#include "Based.h" // Contains definition of Based class
#include "Derived.h" // Contains definition of Derived class

Derived* boo();

Based* foo2()
{
    Derived *a = boo();
    Based *b = (Based*)a;
    return b;
}

Both sources will compile! But foo1() uses different kind of cast then foo2()! First case is bad and will work only in exceptional cases (but even this is not defined by standard).

In other words, in foo1() you tell the compiler "treat pointer of Derived as if it would be pointer of Based", but in the second case you tell the compiler "cast Derived to it's parent, Based".

foo1() might randomly crash or lead into very weird behavior of your code.

And btw. your compiler will probably never warn you about that. So, avoid C-style casting!