Re: Registering a data class with a handler class
Chris Jewell wrote:
Hi Victor,
Thanks for your reply. I'm very new to using classes really.
You're welcome. We've all been there.
[..]
class EpiMath // The Handler class
{
private:
public:
EpiMath();
~EpiMath();
inline double beta_ij(const int&, const int&, const vector<double>&,
EpiCovars&);
Now, this seems strange. If you intend the function to be inline,
you need to provide its body. If you don't provide its body, don't
try to pretend by declaring it 'inline'. If you do provide its body
here, there is no need for the 'inline' keyword -- any function
defined in the class definition is 'inline'.
Yup, the body is defined in another file as inline double
EpiMath::beta_ij...etc. Does this mean that I don't need the 'inline'
modifier in the class definition?
It is most likely *ignored* by the compiler (since no body is supplied),
so, yes, it's safe to say that you don't need it.
class EpiMath
{
private:
EpiCovars& myData;
public:
EpiMath(const EpiCovars& dataRef) {
myData = dataRef;
}
Two points: the argument has to be a ref to non-const EpiCovars and
you need to *initialise* 'myData', not try to *assign* to it:
EpiMath(EpiCovars& dataRef) : myData(dataRef) {}
Yup, I can see that now. What I'm trying to do is to get across the
idea that once initialized, the EpiCovars object that I'm
passing-by-reference to the c-tor of EpiMath is not to be modified.
Is this the correct way to do it?
You're on the right track.
I note from your later post that
you think that there are no member functions in EpiCovars that could
be called for a const object: the EpiCovars::dist(const int&,const
int&) method returns a distance (stored in the object). If it only
returns the distance, and leaves all the data unmodified, why can't
it be called for a const object?
Because that member function is not declared 'const'. You need to also
read up on const-correctness. For now here is an example of what you
want to do:
class EpiCovars {
...
double dist(const int&, const int&) const ; // notice 'const' there
// ^^^^^ <<-------------~~~---+
};
class EpiMath {
const EpiCovars& myData; // notice 'const' here as well
public:
EpiMath(const EpiCovars& mD) : myData(mD) {}
};
Now, it's all proper. Don't forget to repeat the trailing 'const' when
you define the 'dist' member for 'EpiCovars'.
Read up on constructors, initialiser lists, references...
I'm trying......
Good.
Good luck!
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask