Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Registering a data class with a handler class

Reply
Thread Tools

Registering a data class with a handler class

 
 
Chris Jewell
Guest
Posts: n/a
 
      07-20-2006
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-20-2006
Chris Jewell wrote:
> 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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
Guest
Posts: n/a
 
      07-20-2006
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?

Thank you,

--
Ney André de Mello Zunino
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-20-2006
Ney André 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


 
Reply With Quote
 
Chris Jewell
Guest
Posts: n/a
 
      07-20-2006
Hi Victor,

Thanks for your reply. I'm very new to using classes really.

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


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?

>>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? 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
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-20-2006
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


 
Reply With Quote
 
Chris Jewell
Guest
Posts: n/a
 
      07-21-2006
Ok, it's all becoming clear now. Thanks Victor!

Chris
 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      07-21-2006

Chris Jewell wrote:

> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache/mod_python: Registering a request handler dynamically Samuel Python 0 12-28-2008 07:29 PM
Event Handler that creates adds another event handler kaczmar2@gmail.com ASP .Net 1 02-22-2007 07:37 AM
Re: registering/re-registering ASP.NET 1.1.4322 with IIS Juan T. Llibre ASP .Net 2 12-16-2006 12:29 AM
Problem with IE6 not registering an event handler Andrew Ip Javascript 4 11-29-2006 03:05 PM
how do u invoke Tag b's Tag Handler from within Tag a's tag Handler? shruds Java 1 01-27-2006 03:00 AM



Advertisments