问题
I am currently trying to figure out how to initialize variables based on conditions. So this is the current code that I want to modify:
int dimsOut[4];
dimsOut[0] = data->nDataVar();
dimsOut[1] = dims[0];
dimsOut[2] = dims[1];
dimsOut[3] = dims[2];
const size_t dataSize = data->primType().getTypeSize() * dimsOut[0] * dimsOut[1] * dimsOut[2] * dimsOut[3];
Since this is part of a giant project (mostly C++98 with some parts of C++03) I want to try to modify as less as possible to avoid any problems in the rest of the code.
So what I want to do is simply in case data->nDataVar()
returns 1 that the code above executes and in case it returns something else it should
basically do this
int dimsOut[3]; dimsOut[0] = data->nDataVar(); dimsOut[1] = dims[0]; dimsOut[2] = dims[1]; const size_t dataSize = data->primType().getTypeSize() * dimsOut[0] * dimsOut[1] * dimsOut[2];
I am aware that it is not possible to use if-statements since the variables would go out of scope.
Edit: I solved my problem now. It is not beautiful, but it does what it is supposed to do.
Edit2: small change
int decide_dimension = data->nDataVar();
std::vector<int> dimsOut;
dimsOut.resize(3);
dimsOut[0] = dims[0];
dimsOut[1] = dims[1];
dimsOut[2] = dims[2];
if (decide_dimension != 1)
{
dimsOut.push_back(data->nDataVar());
}
const size_t dataSize = data->primType().getTypeSize() * dimsOut[0] * dimsOut[1] * dimsOut[2] * ((decide_dimension == 1) ? 1 : dimsOut[3]);
回答1:
You can use the ternary or conditional operator. The basic form is:
condition ? valueIfTrue : valueIfFalse
Example:
const char* x = (SomeFunction() == 0) ? "is null" : "is not null";
When SomeFunction()
returns 0, x
is initialised with "is null
", otherwise
with "is not null
".
回答2:
What you want to achieve is not possible. The only thing you can do, is to initialize them with a ternary as suggested or move the initialization into a if
block and leave the declaration out.
回答3:
You say you are modifying an existing old project. In that case it makes sense to keep changes minimal.
However, you can't define the size of a static array at run time. If you want, you can keep the array as it currently is and make sure you don't use the 4th element when data->nDataVar() != 1.
Then:
const size_t dataSize = data->primType().getTypeSize() * dimsOut[0] * dimsOut[1] * dimsOut[2] * (data->nDataVar() != 1 ? 1 : dimsOut[3]);
It's worth mentioning the dimsOut array seems completely unnecessary to calculate the value of dataSize, but who knows what else your code is doing with it. If it is only used inside a single function/method then you could easily replace it with something else, such as std::vector.
回答4:
Your question is a bit confusing, or rather not enough information was provided. It is not clear if the dimsOut[]
variable is needed elsewhere. In case it is used ONLY for the computation of dataSize
, you can use directly the dims[]
array and do:
int typeSize = data->primType().getTypeSize() * data->nDataVar();
int dimension = dims[0] * dims[1] * ((condition) ? dims[2] : 1);
const size_t dataSize = typeSize * dimension;
In case that dimsOut is used elsewhere, then you can use the first block modifying the dimsOut[3] assignment with the ternary operator:
int dimsOut[4];
dimsOut[0] = data->nDataVar();
dimsOut[1] = dims[0];
dimsOut[2] = dims[1];
dimsOut[3] = (condition) ? dims[2] : 1;
const size_t dataSize = data->primType().getTypeSize() * dimsOut[0] * dimsOut[1] * dimsOut[2] * dimsOut[3];
来源:https://stackoverflow.com/questions/51555199/c-initialize-variable-based-on-condition