Re: Registering a data class with a handler class
Ney Andr9 de Mello Zunino wrote:
Victor Bazarov wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask