style question - using static variables in function

A

Angus

Hello

I have a function which takes samples and creates an average value.
The function needs to know the number of instances passed until a
'window' value is reached. The window value is 5 so in my calling
function I currently do this:

static long iterations = 0;
++iterations;
sample_average(x, y, iterations);

sample_average is called elsewhere in the program so I can't put the
static long declaration in there. It just looks bad style to me so
was wondering if anyone had a better suggestion for how to handle
this?

In addition, when iterations overflows to 0 then will start count to 5
again.

Puzzled Me.
 
S

SG

Hello

I have a function which takes samples and creates an average value.
The function needs to know the number of instances passed until a
'window' value is reached.  The window value is 5 so in my calling
function I currently do this:

static long iterations = 0;
++iterations;
sample_average(x, y, iterations);

sample_average is called elsewhere in the program so I can't put the
static long declaration in there.  It just looks bad style to me so
was wondering if anyone had a better suggestion for how to handle
this?

Unfortunately, it's not clear to me what you're doing and what it is
that you want to achieve. What is sample_average supposed to do? Is
the definition "static long iterations = 0;" at namespace scope?
What's the meaning of "window value"?

I'm almost sure you can get rid of the static long variable -- and you
should.

Cheers!
SG
 
P

paulkp

was wondering if anyone had a better suggestion for how to handle
this?

Yes, it's called a class. Having iterations as a member variable
is cleaner solution and better programming style.
 
D

Daniel Pitts

Angus said:
Hello

I have a function which takes samples and creates an average value.
The function needs to know the number of instances passed until a
'window' value is reached. The window value is 5 so in my calling
function I currently do this:

static long iterations = 0;
++iterations;
sample_average(x, y, iterations);

sample_average is called elsewhere in the program so I can't put the
static long declaration in there. It just looks bad style to me so
was wondering if anyone had a better suggestion for how to handle
this?

In addition, when iterations overflows to 0 then will start count to 5
again.

Puzzled Me.
I suggest using a Functor object instead of a function+static.

// sample_averager.h
class sample_averager
{
private:
long iterations;
public:
int operator()(Sample x, Sample y) {
++iterations;
// handle sampling.
}
};


extern sample_averager sample_average;

// sample_averager.c++
#include "sample_averager.h"

sample_averager sample_average;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top