Registering a data class with a handler class

C

Chris Jewell

Hi,

I'm wondering what the best way of registering a data storage class with
a data handler class is. At the moment I have two classes:


class EpiCovars // Storage class
{

....

public:

// Public methods //
public:
EpiCovars();
~EpiCovars();
int init(const int, const char*, const char*);
double dist(const int&,const int&);

};


class EpiMath // The Handler class
{
private:

public:

EpiMath();
~EpiMath();
inline double beta_ij(const int&, const int&, const vector<double>&,
EpiCovars&);

};


As you can see, at the moment, I pass a reference to the EpiCovars class
when I call EpiMath::beta_ij. However, the data in the EpiCovars class
does not change after it is initialized, so passing the reference each
time the handler method is called seems silly. What I need to do is to
register a reference to the EpiCovars class when the EpiMath class is
initialized. What is the best way to go about this? I tried:

class EpiMath
{
private:
EpiCovars& myData;

public:
EpiMath(const EpiCovars& dataRef) {
myData = dataRef;
}
};

but this definitely did not work. Any suggestions on what I should do?

Thanks,

Chris Jewell
 
V

Victor Bazarov

Chris said:
I'm wondering what the best way of registering a data storage class
with a data handler class is.

Generally speaking there is no way in C++ to "register a class" (using
C++ definition of a "class") because a class is not an object and does
*not* really exist at run-time and registering usually a run-time
activity. If I misunderstood your intentions, sorry, and do tell.

What you usually do is register an *object* of a certain class. And in
that case the object is usually of a base polymorphic class to allow
overriding (or simply implementing) the proper functionality.
At the moment I have two classes:


class EpiCovars // Storage class
{

...

public:

// Public methods //
public:
EpiCovars();
~EpiCovars();
int init(const int, const char*, const char*);
double dist(const int&,const int&);

};


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'.
};


As you can see, at the moment, I pass a reference to the EpiCovars
class when I call EpiMath::beta_ij. However, the data in the
EpiCovars class does not change after it is initialized, so passing
the reference each time the handler method is called seems silly.
What I need to do is to register a reference to the EpiCovars class
when the EpiMath class is initialized. What is the best way to go
about this? I tried:

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) {}
};

but this definitely did not work. Any suggestions on what I should
do?

Read up on constructors, initialiser lists, references...

V
 
?

=?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=

Victor said:
Chris Jewell wrote:
[snip]
class EpiCovars // Storage class
{

...

public:

// Public methods //
public:
EpiCovars();
~EpiCovars();
int init(const int, const char*, const char*);
double dist(const int&,const int&);

};
[snip]
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) {}

Victor, I would just like to make sure I understand the reasoning behind
your first advice. When you say "the argument has to be a ref to
non-const EpiCovars", you mean that, otherwise, EpiMath would only be
able to use the const interface of the EpiCovars class which, based on
the class definition provided, is empty. Is that all there is to it or
am I missing something else?

Thank you,
 
V

Victor Bazarov

Ney said:
Victor said:
Chris Jewell wrote:
[snip]
class EpiCovars // Storage class
{

...

public:

// Public methods //
public:
EpiCovars();
~EpiCovars();
int init(const int, const char*, const char*);
double dist(const int&,const int&);

};
[snip]
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) {}

Victor, I would just like to make sure I understand the reasoning
behind your first advice. When you say "the argument has to be a ref
to non-const EpiCovars", you mean that, otherwise, EpiMath would only
be able to use the const interface of the EpiCovars class which,
based on the class definition provided, is empty. Is that all there
is to it or am I missing something else?

The 'EpiMath' class contains a member of type "a reference to non-const
EpiCovars". Such reference cannot be initialised with a reference to
a *const* EpiCovars without a const_cast. A const_cast is the last
thing you want to use here.

If 'EpiMath's implementation (or, rather, the 'EpiCovars' interface)
requires the object to be non-const, the object (and the argument) has
to be non-const. Looking at the 'EpiCovars' class confirms that there
aren't any member function that 'EpiMath' could call for a const object.
Hence it has a ref to non-const member. Hence to initialise it a ref
to non-const must be passed to the c-tor. Hence the c-tor's interface
needs to be changed. Hence my advice.

V
 
C

Chris Jewell

Hi Victor,

Thanks for your reply. I'm very new to using classes really.
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?
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? 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?
Read up on constructors, initialiser lists, references...

I'm trying......


Thanks,

Chris
 
V

Victor Bazarov

Chris said:
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.
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'.
I'm trying......

Good.

Good luck!

V
 
N

Noah Roberts

Chris said:
class EpiMath
{
private:
EpiCovars& myData;

public:
EpiMath(const EpiCovars& dataRef) {
myData = dataRef;
}
Change to:
EpiMath(EpiCovars & dataRef) : myData(dataRef) {}

A reference cannot exist without a value. By the time the constructor
body is called the reference exists. Using initializers gives that
reference a value upon creation. Problems solved. You already got
your answer about the const so I won't reiterate.
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top