Can somone help explain some fundementals of c++? I keep seeing this stuff over and over and just want that "push" to help me understand.
-
So I understand what everything does in the code below. However I don't understand the reasoning behind it.
- Why is "const" used the way it is used? I feel like const is just "peppered" throughout c++ and I never know why its being used.
- why does the constructor take in a reference to a string, and a pointer to a Widget? (whats the explicit reasoning for using each ?)
this code is from the fridge magnet example :
class DragLabel : public QLabel
{
public:
DragLabel(const QString &text, QWidget *parent);
QString labelText() const;private:
QString m_labelText;
}; -
I will try:
DragLabel(const QString &text, QWidget *parent);
const is used to tell compiler that the value is not going to be changed inside, which allows it to build more efficient code.
Story starts from the fact that you want to pass existing string to function.But if you write
- DragLabel( QString text, QWidget *parent);
Compile will have to copy string value which is slow and it is not needed.
in such cases you pass const references and it means that no copy occurs and from within function actually an address ( reference is an address ) will be used to access the value of existing outside QString object.
Why QWidget *parent)?
Cause reference is an address to existing object. Parent may or may not be defined and thus pointer is used.
Pointer is also an address, but it may point to NULL when reference must always point to valid object.(Technically you are able to achieve what is strictly forbidden and get reference to invalid or NULL pointer using language features, but if you find such code in real life you better stay away from such programmer as far as you can)
Hope this helps, but book would describe this much better.
- DragLabel( QString text, QWidget *parent);
-
Hi,
@Rample said:
- Why is "const" used the way it is used? I feel like const is just "peppered" throughout c++ and I never know why its being used.
These articles might help:
- http://www.cprogramming.com/tutorial/const_correctness.html
- http://stackoverflow.com/questions/136880/sell-me-on-const-correctness
- why does the constructor take in a reference to a string, and a pointer to a Widget? (whats the explicit reasoning for using each ?)
There are 2 categories of classes: Value classes and Identity classes.
- Value objects are simply data: strings, timestamps, images, and so on. You can copy the data inside value objects without any issues.
- Identity objects, on the other hand, are not copyable.
- Take QFile for example -- what should happen if you "copy" a QFile object? Should the file on actual disk also be copied? Or should the actual file be left alone, and just have two QFile objects accessing the same file? Neither option is sensible, so Qt disallows you from copying the QFile.
In your example, QString is a value class and QWidget is an identity class. (In fact, all QObjects are identity classes.) In the Qt API:
- Value objects are passed by-const-reference. Think of passing by const-reference as a safer and more efficient form of passing by-value ("copying" without actually creating a copy)
- Identity classes are passed around by-pointer. They cannot be copied, therefore they cannot be passed by-value.
See also: Qt Objects: Identity vs Value
-
Why parent ptr?
Parent child relationship - http://doc.qt.io/qt-5/objecttrees.html