How do I make a member in my class an array

So I have this class Gaus. I want Gaus to have a member lambda which is of type
double[]. How do I say that in my .h file. Do I have to commit to a certain array (i.e do some initialization) or can I wait till someone calls my constructor where they pass in an array and the size of the array.

for instance can I do:

1
2
3
4
5
6
class Gaus {
private:
	double lambda[];
public:
	Gaus (double lam[], int size);
}
If you're defining it like that, you have to provide an array size in between the brakets of lambda.

1
2
3
4
class Gaus {
    private:
        double lambda[32];
    //... 

If you want to have an array with variable size, you'll have to use a double pointer and the new[] operator.
Here's the problem if I make lambda a pointer to doubles then I can't call this in main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cmath>
#include "Gaus.h"
using namespace std;



int main()
{
	Gaus var;
	double lam[] = {0.00, 1.00, 2.00};
	var.setLambda(lam, 3);
	cout << sizeof (var.getLambda ());
	for (int i = 0; i < sizeof (var.getLambda ())/sizeof (double); i++)
	{
		cout << (var.getLambda ())[i] << endl;

	}
	int x;
	cin >> x;
	return 0;
}
The sizeof trick only works with static arrays, not pointers. A std::vector will be better.
thanks!
Topic archived. No new replies allowed.