-4

I am new to Qt and have trouble understanding the help document, wish some one can help, much appreciated.

For example:

void QList::append(const T & value)

I can see the return type is void, and it is a member function of QList. But what in the brackets really confused me, what does it mean by const T & value ? I tried using a regular QString variable as argument and it worked fine, then why const?

Another example is

 void QFile::setFileName(const QString & name)

The sample code provided in the help document is:

QFile file;
file.setFileName("readme.txt");

I can not figure out what QString & name means, does it have something to do with reference?

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • The `const` just means that the method itself can not modify it, so the `QString` will be the same before and after the function call – msrd0 Sep 28 '14 at 18:59
  • 3
    Please get a [good book on C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Captain Obvlious Sep 28 '14 at 18:59
  • 6
    This question appears to be off-topic because it is about understanding C++ instead of a specific problem with code. – Captain Obvlious Sep 28 '14 at 19:01
  • @CaptainObvlious How is "understanding C++" off topic at SO?! I don't think this question is *so* basic, that it should be closed because of that. Templates and const semantics of C++ are rather complex. – hyde Sep 29 '14 at 11:54
  • 1
    @hyde While const correctness can certainly be a heavy topic, it's not in this case. This is just a request for an explanation of C++ basics. Topics such as the one in this post are covered by books and tutorials which are readily available. However, giving it and you the benefit of the doubt I have voted to reopen. – Captain Obvlious Sep 29 '14 at 14:34
  • The thing is that I have trouble define my question clearly due to lack of knowledge in C++ basics terms. Thank you all for the patience, I know where to start now. – No harmer Sep 30 '14 at 15:01

1 Answers1

3

First of all Qt is a framework, Qt Creator is an IDE. I understand you are a beginner and these things should look confusing but I recommend taking your time and starting learning them step by step.

What you are really confused of is C++ templates, not Qt.

T is a generic type and can be specified with the diamond operator(<>) like this: QList<int> integerList;. Now if you execute integerList.append(42) the T out there will be interpreted as int. If the variable type passed to append function is different than the type specified in QList<int> integerList; you will get an error.

The second problem you have is you don't understand references.

I recommend reading a good C++ book and then continue on. Or at least a decent language tutorial...

Lastly, this is a forum where people post problems on the code they are working on or issues they have with the code they have already written. You won't find anybody starting to teach you a programming language, that's why you got so many minuses. Here I recommend the stackoverflow help page.

Community
  • 1
  • 1
Jacob Krieg
  • 2,834
  • 15
  • 68
  • 140
  • *If the variable type passed to [...] you will get an error.* Only if conversion is not possible. You can, for example, pass a `char` to `QList::append(int)` just like you can do with regular functions taking an `int`. – leemes Sep 29 '14 at 08:19
  • 1
    Yes, but that's a particular case called implicit conversion. If you start telling him about all the exceptions from the rule you'll confuse him even more. He doesn't know the basics so I pointed out the problems as simple as I could and recommended books/tutorials for him to read. – Jacob Krieg Sep 29 '14 at 08:23
  • Good point about simplicity. – leemes Sep 29 '14 at 08:25
  • About "you won't find anybody starting to teach you a programming language", that's a rather blurry line. For example, SO is full of highly voted questions about C++11 asking people to explain details of basic concepts. Of course "new features of C++11" is rather more interesting that "features of C++98", but they're still same type of question. – hyde Sep 29 '14 at 11:59
  • 1
    By "you won't find anybody starting to teach you a programming language" i referred to the fact that nobody will teach you a programming language from A to Z. Asking for more explanations about C++11 features or some exceptions to the rule is not learning a programming language. – Jacob Krieg Sep 29 '14 at 12:30