Re: passing unnamed array to function
poorboywilly wrote:
I've been unable to find any information on this through any search
engines. Is there any way to pass an unnamed (temporary) array to a
function?
There are no "array literals" in C++ (beyond arrays of 'char const').
You can probably create a way. Here is what you need to do. Define
a class (better a template) which will have a constructor with '...'
for the argument. It will manage its own memory, say. A member of
that class will be the pointer to the dynamic memory, so you would
be able to use syntax like
f1(make_array(1,2,3,4).pointer);
It might look something like
template<class T> struct make_array_t {
T *pointer;
make_array_t(T t ...); // the constructor - allocates and
// fills the memory
~make_array_t();
private:
// those are prohibited
make_array_t& operator=(make_array_t const&);
make_array_t(make_array_t const&);
};
// and here is the factory:
template<class T> make_array_t<T> make_array(T t ...);
All you need to figure out is how you manage the variadic arguments
(how you stop processing the list, that is).
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask