I have this function:
void RegMatrix::MatrixInit(int numRow,int numCol, std::vector<double> fill)
{
do something;
}
and i want to send this:
MatrixInit(numRow,numCol,NULL);
how can i pass NULL as vector?
You can't have a null vector in C++. C++ is a value-type language … a variable of type vector<int> contains the value of vector<int> directly, NOT a reference to a vector like in java. So, you declare a vector, you get an empty vector. There is no such thing as a null vector.
When we pass an array to a function, a pointer is actually passed. However, to pass a vector there are two ways to do so: Pass By value. Pass By Reference.
Explanation to the Code:Declare using std::cout and std::vector. Declare function with vector type return type which accepts vector as a variable. Then return null vector inside function body. Declare main function.
A vector cannot possibly be NULL, but it can be empty(). No. NULL is normally used with a pointer. You could however return an empty vector and verify on the other side if it's empty.
You cannot pass NULL as vector, you could instead pass an empty vector like this:
MatrixInit( numRow, numCol, std::vector<double>() )
Note that you would be better off taking the fill
vector as const&
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With