I have an array with an unknown number of elements and want to know ne number of entries.
Here's my code:
Code:
#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
class nvec{
private:
double *a;
int length;
public:
nvec(int size, double*p)
{
length = size;
a = new double [length];
for(int i = 0;i<length;i++)
{
a[i] = p[i];
}
}
//~nvec();
double Len(void);
};
double nvec::Len(void)
{
double length = sizeof(a)/sizeof(double);
return length;
}
int main()
{
double t[]={2.,9.,12.,1.,113.};
nvec A(sizeof(t)/sizeof(double),t);
A.Print();
cout<<" :"<<A.Len()<<endl;
return 0;
}
However, I only get 1 as the length of the array. I guess this is because sizeof gives me just the memory of one entry.
What would be the correct way to accomplish what I try?