I am a total C++ noob, and I am having some trouble returning an array from my methods. I have a header file with the following method declaration:
virtual d
To answer your question more directly, if you declare an array of doubles as follows:
double doubleArray[] = {1.0, 2.0, 3.0};
you would declare the method like this:
virtual double *
echoDoubleArray(double* doubleArray, int arraySize) throw (RemoteException);
Arrays are passed as pointers. If you want to use the true power of C++, then follow the advice provided in the other answers.
Sorry everyone seems to be telling you to use modern STL type programming instead of answering your question. They are right. But you should still understand how things work. The problem with returning arrays are that arrays created in your function will be on the stack. When you leave your function they will not exist anymore. What you can do is return a pointer to an array on the Heap. Research a little more about the stack and the heap to understand. But below is a simple C++ function returning an array allocated on the Heap.
#include <iostream>
double * MakeArray()
{
double *mydouble = new double[5];
mydouble[1]=1.1;
mydouble[1]=2.2;
return mydouble;
}
int main()
{
double *d=MakeArray();
std::cout<<d[1]<<d[2];
delete[] d;
}
edit:
Thinking about it some more, another way to do it without STL would be returning a struct with an array in it. Most compilers will return a struct by value (assuming your not doing embedded programming).
Probably the best simple way: Pass your array into the function. It is like passing a reference because you are really just passing a pointer.
#include <iostream>
void MakeArray(double d[5])
{
d[0] = 0.0; d[1] = 1.1;
d[2] = 2.2; d[3] = 3.3; d[4] =4.4;
}
int main()
{
double dub[5];
MakeArray(dub);
std::cout<<dub[1]<<dub[2];
}
After understanding how everything works. go use the STL.
You'll better use std::vector or if you really want to return a plain array, return a pointer (to such an array) dynamically allocated with e.g.
double* arr = new double[arrSize];
// fill arr appropriately
return arr;
But I'll recommend returning a std::vector<double>
instead.
C++ doesn't play nice with non-local arrays, more likely you should be using an actual container like std::array<double,10>
or std::vector<double>
. I don't think it's possible to return an array from a function.
The C++ syntax for arrays is: std::vector<double>
instead of double[]
. It also requires you to put #include <vector>
near the top of your source file. Other than that they work very similar to C-arrays.
There is a technique for passing arrays that do not decay to pointers. You pass the array by reference, using the syntax below. The required "extra" parens are a syntactical wart, and probably why this usage is not more common. One gets used to it real fast, given the advantages.
void sub(double (&foo)[4])
{
cout << sizeof(foo) << endl;
}
Prints 32, not sizeof(double*), because the type of foo really is a reference to an array of 4 doubles. Using templates, you can generalize to work for any size of array:
template <int N> sub(double (&foo)[N])
{
cout << sizeof(foo) << endl;
}
double bar[5];
double zot[3];
sub(bar); // ==> 40
sub(zot); // ==> 24
You can generalize again to handle an any-size array of anything:
template <class T, int N>void sub(T(&foo)[N])
{
cout << sizeof(foo) << endl;
}
double bar[5];
char zot[3];
sub(bar); // ==> 40
sub(zot); // ==> 3
and you now have something that captures the abstract idea of "sub" regardless of the type or size of the array you pass it.