01-13-2013 12:07 PM
Hey guy,
Another C# newbie question, I checked out some samples that uses 'const type &variable_name'.
So I thought... hmm what is that... I found out that const is a constant, but... after that I thought that thinking it as constant, it made the things a bit weird to understand, why should I declare constants on functions variables??
My examples before and after had no difference for me:
Before:
MyPaths::MyPaths(QObject *parent, QVariant *currentRoute) : QObject(parent)
{
...
favorite = *currentRoute.toMap()["favorite"].toBool();
}
After:
MyPaths::MyPaths(QObject *parent, const QVariant ¤tRoute) : QObject(parent)
{
...
favorite = currentRoute.toMap()["favorite"].toBool();
}
I think the before was theorically wrong... but the effect was the same... can anyone explain to me if there's a right or wrong on these two?
Solved! Go to Solution.
01-13-2013 12:33 PM - edited 01-13-2013 12:38 PM
QVariant *currentRouter declares a pointer to QVariant.
Pointer to QVariant is a memory address pointing to object of type QVariant.
So you're passing not the QVariant instance itself, but it's memory address.
* operator dereferences memory address. So result of (*currentRoute) is object itself. You can also encounter this style: currentRoute->name. This is essentially the same.
The pointer variable itself is not constant - you can assign address of another QVariant to it.
It can be made constant, but there's no reason to do this. It's just a local variable holding address.
On the second example you're passing object by reference. Reference is like another name for the object. It behaves exactly like the original object. It can't be NULL, it always exists. You work with reference just like you work with original object.
References are initialized on creation and can't be reassigned later. So references are always constant.
In this case reference points to constant object, so you can't modify the object via this reference.
const QVariant ¤tRoute
Some more examples:
QVariant *currentRoute;
Non-constant pointer to non-constant object.
Object can be modified: currentRoute->setXxx();
Pointer can be modified: currentRoute = &someOtherObject;
const QVariant *currentRoute;
Non-constant pointer to constant object.
Object can't be modified: currentRoute->setXxx();
Pointer can be modified: currentRoute = &someOtherObject;
const QVariant *const currentRoute;
Constant pointer to constant object.
Neither can be modified.
QVariant ¤tRoute = someObject;
Reference to non-constant object.
Object can be modified: currentRoute.setXxx();
Reference can't be modified after initialization.
const QVariant ¤tRoute = someObject;
Reference to constant object.
Object can't be modified: currentRouter.setXxx();
Reference can't be modified after initialization.
Which one to prefer?
Well, if you need a way to pass NULL value into function then use pointer.
In most cases references are sufficient.
Compiler usually generates the same code for references and pointers.
Pointers were used actively in C, references were introduced later in C++.
01-13-2013 01:22 PM
Hmm, so basically, there isn't a wrong code pieces among those two?! Just the way I need to use it that will differ how I can use reference or pointer.
Thanks for your reply, it is very good to understand in a very simple answer, what I was reading until now for about 4 hours and being more and more confused!!!
01-13-2013 01:32 PM - edited 01-13-2013 01:36 PM
That's right, both are correct.
But when using pointers NULL value could be passed.
It's not a problem if this is your own application and you are sure that you won't pass NULL.
Otherwise it's better to check for this case explicitly (see example below) or use references.
Also you can declare the object 'const' to protect it from modifications:
MyPaths::MyPaths(QObject *parent, const QVariant *currentRoute) : QObject(parent)
{
...
favorite = currentRoute ? currentRoute->toMap()["favorite"].toBool() : false;
}
When object is declared as const only const member functions can be called:
int getSomething() const; // ok
void setSomething(); // can't be called