On private class member variable as pointer to double and its application

Hi there,

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.

Here we have the Header file, Random.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 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:
	
		unsigned int _a;         /* parameters needed to generate series of number */
		unsigned int _c;
		unsigned int _m;
		unsigned int _seed;


		double* vec;
                int _nvar;
		
};



Here we have function implementation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37


Random::Random(int seed) {
	
	_seed = seed;
	_a = 1664525; 
	_c = 1013904223; 
	_m = pow(2,31);

};


void Random::Storing(int n) {
	
	_nvar = n;
	int i = 0;	
	vec = new double[_nvar];     /* is this legal? vec is my private member variable */

	int stop;

		
        do {									
	
			unsigned int x = (_a*_seed + _c) % _m;   /* methods to casually generate numbers between 0 and 1 (Linear Cingruential Generator) */
			_seed = x;
	
			vec[i] = (double(x)/double(_m-1));
			
			i++;
			stop = i;
			
	} while(stop < _nvar);



}



If this is even possible, i could calling it via my already defined Random class object, using my private double vec pointer.

Hope you guys will illuminate me. Have a good day!
Last edited on
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?
Hi Repeater, thanks for the reply.

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:

Random.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Random {

	public:
		Random(int seed);

		
		void Storing(int);
		void Sum(int);
		void Variance();
		
	private:
	
		unsigned int _a;
		unsigned int _c;
		unsigned int _m;
		unsigned int _seed;
		
		double* vec;
		double _sum;
		int _nvar;
		int _nsum;
		double _variance;
		double _sumvariance;

};


Random.c will be smth like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
void Random::Storing(int n) {
	
	_nvar = n;
	int i = 0;	
	vec = new double[_nvar];

	int stop;

		do {									
	
			unsigned int x = (_a*_seed + _c) % _m;
			_seed = x;
	
			vec[i] = (double(x)/double(_m-1));
			
			i++;
			stop = i;
			
		} while(stop < _nvar);



}


void Random::Sum(int n) {

	_nsum = n;
	_sum = 0;
		
		for(int i = 0; i < _nsum; i++) {
	
			_sum += vec[i];
	
		}

	
cout<<_sum<<endl;
	
}


void Random::Variance() {

	std::cout<<"Variance on generated number series : "<<std::endl;
	
		double meanvar = 0;
		_variance = 0;

			for(int k = 0; k < _nvar; k++) {

				meanvar += vec[k];
			}
		
		meanvar = meanvar/_nvar;
 
				for(int i = 0; i < _nvar; i++) {
		
					_variance += pow(vec[i] - meanvar, 2);
				
			
				}

		_variance = _variance/_nvar;
		std::cout<<_variance<<std::endl;

	std::cout<<std::endl;
	std::cout<<std::endl;

	std::cout<<"Variance on Sum series: "<<std::endl;
		
		double meansum = _sum/_nsum;
		_sumvariance = 0;
		
			for(int j = 0; j < _nsum; j++) {
			
				_sumvariance += pow(vec[j] - meansum, 2);
	
			} 
		
		_sumvariance = _sumvariance/_nsum;
		std::cout<<_sumvariance<<std::endl;
				

}
 


As you can see, I need to recall elements in these other class methods. Do you think there is a more efficient way to do this?


I need that vector to store elements and valuating sum and variance of those.

Except it's not a vector. You should use a vector. http://www.cplusplus.com/reference/vector/vector/

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.

1
2
3
4
double getVariance()
{
  return _variance;
}
Last edited on
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.

Last edited on
Hi guys! Thanks for the info!
I have lot to meditate about now, thanks again!
Topic archived. No new replies allowed.