Const Static variables set at run time and a design question.

J

Jim

Hi,
I've a set of data which has two spectra associated with each
position. For each spectra you convert from an array position to a
real value using an array offset, a delta and a velocity offset.
These are common to all spectra of the same type and don't change
through the program's run. Unfortunately I don't know them until I
load the file, unless I hard code them and I don't want to do that.
Only solution I can think of is to have a user-defined object called
conversion which has const members set in the constructor and const
accessor methods and have a static object in each type of the specific
spectra classes which inherit from the spectra base class. Does this
seem correct?

Thanks,
Jim
 
M

Marcel Müller

Hi,
I've a set of data which has two spectra associated with each
position. For each spectra you convert from an array position to a
real value using an array offset, a delta and a velocity offset.
These are common to all spectra of the same type and don't change
through the program's run. Unfortunately I don't know them until I
load the file, unless I hard code them and I don't want to do that.

so they are NOT const static because they change while the application runs.
Only solution I can think of is to have a user-defined object called
conversion which has const members set in the constructor and const
accessor methods and have a static object in each type of the specific
spectra classes which inherit from the spectra base class. Does this
seem correct?

Something like that is possible. But I would keep conversion as simple
as possible:

struct conversion
{ double offset;
double whatever;
};

class SpectraBase
{
public virtual const conversion& GetConversion() const = 0;
...
};


Marcel
 
V

Victor Bazarov

Jim said:
I've a set of data which has two spectra associated with each
position. For each spectra you convert from an array position to a
real value using an array offset, a delta and a velocity offset.
These are common to all spectra of the same type and don't change
through the program's run. Unfortunately I don't know them until I
load the file, unless I hard code them and I don't want to do that.
Only solution I can think of is to have a user-defined object called
conversion which has const members set in the constructor and const
accessor methods and have a static object in each type of the specific
spectra classes which inherit from the spectra base class. Does this
seem correct?

It doesn't seem /in/correct. Do you see any issue with the solution?
If you have to have a const member, the constructor is the only place to
initialize it. You *could* make it 'mutable' and have a special member
function that would change it, but IME 'mutable' is dangerous because it
breeds temptation to change the member more often than needed.

V
 
J

Jim

It doesn't seem /in/correct.  Do you see any issue with the solution?
If you have to have a const member, the constructor is the only place to
initialize it.  You *could* make it 'mutable' and have a special member
function that would change it, but IME 'mutable' is dangerous because it
breeds temptation to change the member more often than needed.

V

Seems an interesting problem, I'd have thought there would be lots of
occasions where there are values common to all objects of a class
which need to remain const, but aren't known until run time. Storing
three values common to each one of 40,000 spectra seems wasteful. I
was really wondering if there was an equivalent to the following code
but at a class level. It's probably a design issue? In the example m_n
is const, but not known until the object is constructed.

class A
{
const int m_n;
public:
A();
int get() const;
};
A::A( int n ) : m_n( n )
{
}
 
V

Victor Bazarov

Jim said:
Seems an interesting problem, I'd have thought there would be lots of
occasions where there are values common to all objects of a class
which need to remain const, but aren't known until run time. Storing
three values common to each one of 40,000 spectra seems wasteful.

Really? 40 thousand pairs of what, 4-byte numbers? That's 320K.
Wasteful? Maybe. But do you really need to concern yourself with
"wasting" 320K in a modern computer with at least 1 gig of RAM?
> I
was really wondering if there was an equivalent to the following code
but at a class level. It's probably a design issue? In the example m_n
is const, but not known until the object is constructed.

class A
{
const int m_n;
public:
A();
int get() const;
};
A::A( int n ) : m_n( n )
{
}

Uh... I don't understand what is holding you from using a pointer to an
object of type 'A' as a static member of another class?

class Spectrum123
{
static A* m_pA;

Spectrum123(std::istream &is); // acquisition from a file
};

Spectrum123::Spectrum123(std::istream &is)
{
... // do what's needed to get the spectrum read

if (!m_pA)
{
int someValue = ... // calculate from the spectrum
m_pA = new A(someValue);
}
}

V
 
J

James Kanze

I've a set of data which has two spectra associated with each
position. For each spectra you convert from an array position
to a real value using an array offset, a delta and a
velocity offset. These are common to all spectra of the same
type and don't change through the program's run. Unfortunately
I don't know them until I load the file, unless I hard code
them and I don't want to do that. Only solution I can think
of is to have a user-defined object called conversion which
has const members set in the constructor and const accessor
methods and have a static object in each type of the specific
spectra classes which inherit from the spectra base class.
Does this seem correct?

If the data can be made available before entering main, there's
no problem; something like:
int configuration = getIntFromEnvironment( "CONFIGVARNAME" ) ;
works fine. If you have to read the value from a file (whose
name is passed in argv, for example), then it's more difficult.
Perhaps something along the lines of:
ConfigurationData const* configData ;
would do the trick, with configData being set each time after
reading the values.
 

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

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top