Reputation: 26333
I would like to assign a value to a const QList
depending on the value of a pointer.
invar
is a pointer, if it is NULL
I want to assign a first value to const QList mylist
, if it is not NULL
, another value. But const correctness is observed: i do not intend to change value for mylist
afterwards.
Is it possible to have
if(invar)
const QList<Type> zoneList = invar->getZones();
else
const QList<Type> zoneList = aList ;
I am defining zoneList
once exactly, so why would this not be working ?
ALso, as I am using zoneList
in the next part of my method, I get this compile error: undeclared identifier
.
But I am not allowed to write that either
const QList<Type> zoneList ;
if(invar)
zoneList = invar->getZones();
else
zoneList = NULL ;
and I have compiler error Error 1
error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const QList<T>' (or there is no acceptable conversion)
Is it possible to have two different values assigned to this const QList depending on a condition ? What is the right way to write it then ?
Upvotes: 3
Views: 2833
Reputation: 15069
With such a simple condition, you could just use the ternary operator:
const QList<Type> zoneList = invar ? invar->getZones() : aList;
If the initialization gets more complex than that and you happen to use C++11, then you can use a new idiom made possible by lambdas:
const QList<Type> zoneList = [] () -> QList<Type> {
// ^^ possibly [&] to capture variables
// some complex stuff that returns a QList<Type>
}(); // Note the () that calls the lambda immediately
If you are stuck with C++03 (or, Stroustrup forbid, C++98), there are few solutions left except calling another function/method, with the drawback that you have to pass as a parameter any local variable you need to compute the const value.
Edit: I realize I haven't fully answered your question. Others have already explained that your first if
can't work because it's a matter of scope, I won't add much except that adding braces will probably make it clearer for you (note how this is strictly the same code as yours: the braces are optional when the if
/else
bodies are just a single statement but their presence/absence doesn't change the semantics of the code):
if (invar) {
const QList<Type> zoneList = invar->getZones();
}
else {
const QList<Type> zoneList = aList;
}
Upvotes: 5
Reputation: 13207
If you do
if(invar)
const QList<Type> zoneList = invar->getZones();
else
const QList<Type> zoneList = aList ;
the scope of zoneList
is limited to the if
or else
branch, and outside it does not exist anymore.
And if you do
const QList<Type> zoneList ;
if(invar)
zoneList = invar->getZones();
else
zoneList = NULL ;
zoneList
is declared const
and cannot be changed in if
/else
.
For a hack you could use const_cast
, but it is bad practice so you must initialize zoneList
.
zoneList = invar != 0 ? invar->getZones() : 0; //don't use NULL in C++
Upvotes: 1
Reputation: 87957
Use the ternary operator ?:
const QList<Type> zoneList(invar ? invar->getZones() : aList);
BTW is it NULL
or is it aList
? You have both in your code above.
This
if(invar)
const QList<Type> zoneList = invar->getZones();
else
const QList<Type> zoneList = aList ;
doesn't work because each zoneList variable is defined inside the if statement only.
Upvotes: 10