1

Sorry, but I can't find a right place in C++ manual.

In a following string:

void setYRange(QString name1, int s1, int e1, QString name2 = 0, int s2 = 0, int e2 = 0);

I do not understand the initialization:

QString name2 = 0
  • If it is a string, why it is initialized with an integer?
  • If it is an object reference, why it isn't initialized with NULL ?
  • If it creates an object and fills it with 0 bytes, how does it know how much bytes to allocate?

Please give me just a reference to the right place in C++ manual.

Paul
  • 25,812
  • 38
  • 124
  • 247
  • You might look herer (http://stackoverflow.com/questions/12340257/default-vs-implicit-constructor-in-c) for further reference. – maxik May 15 '17 at 14:52

1 Answers1

2

What you are seeing is the compiler choosing the QString::QString(const QChar *unicode, int size = -1) constructor. 0 is the null pointer value so a pointer can be implicitly constructed from it. That means to compiler will chose the c-string constructor and initialize the pointer to a null pointer. Since the pointer is null and that means you will construct an null QString(it is empty).

Do note that this behavior is different from a std::string. Constructing one of those from a null pointer is undefined behavior.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • Does it mean that this 0 will be used as the first parameter of the constructor and not as a value of an object self? – Paul May 15 '17 at 14:45
  • 1
    @Paul Yes. `unicode` is initialized to `0`(null) and size stays `-1`. – NathanOliver May 15 '17 at 14:46
  • `QString::QString(const QChar *unicode, int size = -1)` is explicit. Here another constructor is used. – Meefte May 15 '17 at 16:12
  • @Meefte Looks like they changed that between versions. Look like prior to version 5x it was not. If they are using the 5x version it might be using `QString::QString(QChar ch)` – NathanOliver May 15 '17 at 16:21