I'm actually a bit unsure about the right way to utilize this method. My aim is to store a series of input number via class method into a member variable and then recall some of these numbers using pointers, everything belonging to an object of my class.
I'll put here just an example of the code I'm writing, getting directly to the prblem.
class Random {
public:
Random(int seed);
void SetA(int a) { _a = a;}
void SetC(int c) { _c = c;}
void SetM(int m) { _m = m;}
void SetSeed(int seed) { _seed = seed;}
void Storing(int); /* void function which stores an int number of elements into the private member double* vec */
private:
unsignedint _a; /* parameters needed to generate series of number */
unsignedint _c;
unsignedint _m;
unsignedint _seed;
double* vec;
int _nvar;
};
Why do you need a pointer to the inside of the object? That would allow the class' internals to be changed without the class knowing. It's generally considered bad design, given that a class is meant to encapsulate the internals and make them both opaque and irrelevant to users of the class.
Why are you doing manual memory management instead of using a vector?
Actually implementation file is more complex than this, I always try to make main.cpp as light as possible.
I need that vector to store elements and valuating sum and variance of those.
I won't write the entire Header file because esulates the scope of my question, but I'll add another piece of code to get you understand why I am doing this:
From your class, don't return pointers to values. Return copies.
That said, currently your class provides no functions to return values.
Your class should look after itself. I would expect the constructor to call whatever functions need to be called in order to put itself into a good state; the constructor should call the private functions variance and sum and storing. Public functions should be provided to return a copy of the sum, a copy of the variance, a copy of the numbers.
vec = new double[_nvar]; /* is this legal? vec is my private member variable */
this is legal.
you probably want a destructor that calls delete[] vec.
you probably want self defense in case storing is called more than 1 time and leaks memory.
something like this flow:
constructor: vec = null
storing: if vec, delete [] vec; vec = new... etc
destructor: delete vec
linear cong. random generators have a lot of problems. If you are going to bother writing a new generator, consider something else. Otherwise you may as well just use rand() and save the trouble.