|
|
|
|
|
|
| Author |
Message |
Chris Jewell *nix forums beginner
Joined: 20 Jul 2006
Posts: 3
|
Posted: Thu Jul 20, 2006 10:06 am Post subject:
Registering a data class with a handler class
|
|
|
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 |
|
| Back to top |
|
 |
Victor Bazarov *nix forums Guru
Joined: 07 Apr 2005
Posts: 3949
|
Posted: Thu Jul 20, 2006 12:15 pm Post subject:
Re: Registering a data class with a handler class
|
|
|
Chris Jewell wrote:
| Quote: | 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.
| Quote: | 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'.
| Quote: |
};
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) {}
| Quote: | };
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 |
|
| Back to top |
|
 |
Ney André de Mello Zunino *nix forums beginner
Joined: 15 Mar 2005
Posts: 28
|
Posted: Thu Jul 20, 2006 12:31 pm Post subject:
Re: Registering a data class with a handler class
|
|
|
Victor Bazarov wrote:
| Quote: | Chris Jewell wrote:
|
[snip]
| Quote: | 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]
| Quote: | 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 |
|
| Back to top |
|
 |
Victor Bazarov *nix forums Guru
Joined: 07 Apr 2005
Posts: 3949
|
Posted: Thu Jul 20, 2006 12:45 pm Post subject:
Re: Registering a data class with a handler class
|
|
|
Ney André de Mello Zunino wrote:
| Quote: | 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 |
|
| Back to top |
|
 |
Chris Jewell *nix forums beginner
Joined: 20 Jul 2006
Posts: 3
|
Posted: Thu Jul 20, 2006 3:57 pm Post subject:
Re: Registering a data class with a handler class
|
|
|
Hi Victor,
Thanks for your reply. I'm very new to using classes really.
| Quote: | 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?
| Quote: | 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?
| Quote: | Read up on constructors, initialiser lists, references...
|
I'm trying......
Thanks,
Chris |
|
| Back to top |
|
 |
Victor Bazarov *nix forums Guru
Joined: 07 Apr 2005
Posts: 3949
|
Posted: Thu Jul 20, 2006 4:09 pm Post subject:
Re: Registering a data class with a handler class
|
|
|
Chris Jewell wrote:
| Quote: | Hi Victor,
Thanks for your reply. I'm very new to using classes really.
|
You're welcome. We've all been there.
| Quote: | [..]
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.
| Quote: | 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.
| Quote: | 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'.
| Quote: | 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 |
|
| Back to top |
|
 |
Chris Jewell *nix forums beginner
Joined: 20 Jul 2006
Posts: 3
|
Posted: Fri Jul 21, 2006 7:06 am Post subject:
Re: Registering a data class with a handler class
|
|
|
Ok, it's all becoming clear now. Thanks Victor!
Chris |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|
|
The time now is Sun Nov 23, 2008 1:43 pm | All times are GMT
|
|
Actress | Free phpBB forum | Free Text Messages | Loans | Mobile Phones
|
|
Copyright © 2004-2005 DeniX Solutions SRL
|
|
|
|
Other DeniX Solutions sites:
Unix/Linux blog |
electronics forum |
medicine forum |
science forum |
|
|
Privacy Policy
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|