c++qtqstring

Is there any good reason to default initialize a new QString?


I am confronted with some legacy Qt code and i see a lot things like this:

QString myString = QString();

From my point of view it is unnecessary. Is there any good reason to do so?


Solution

  • No, there is no good reason. This is not specific to Qt, but a generic c++ behavior. Declaring a variable of any class type like this:

    QString mystring
    AnyObject myobject;
    // etc.
    

    will implicitly call default constructor for the corresponding type.

    Edit: as pointed in comment, this is only valid for class types (including structs and unions) but not for basic types (int, char, long, float, etc.).