Registering a data class with a handler class

Discussion in 'C++' started by Chris Jewell, Jul 20, 2006.

  1. Chris Jewell

    Chris Jewell Guest

    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
     
    Chris Jewell, Jul 20, 2006
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Jul 20, 2006
    #2
    1. Advertising

  3. 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
     
    =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=, Jul 20, 2006
    #3
  4. 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
     
    Victor Bazarov, Jul 20, 2006
    #4
  5. Chris Jewell

    Chris Jewell Guest

    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
     
    Chris Jewell, Jul 20, 2006
    #5
  6. 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
     
    Victor Bazarov, Jul 20, 2006
    #6
  7. Chris Jewell

    Chris Jewell Guest

    Ok, it's all becoming clear now. Thanks Victor!

    Chris
     
    Chris Jewell, Jul 21, 2006
    #7
  8. Chris Jewell

    Noah Roberts Guest

    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.
     
    Noah Roberts, Jul 21, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shruds
    Replies:
    1
    Views:
    855
    John C. Bollinger
    Jan 27, 2006
  2. Juan T. Llibre
    Replies:
    2
    Views:
    3,931
    =?Utf-8?B?UmljaA==?=
    Dec 16, 2006
  3. Replies:
    1
    Views:
    717
    Damien
    Feb 22, 2007
  4. Samuel
    Replies:
    0
    Views:
    256
    Samuel
    Dec 28, 2008
  5. Andrew Ip
    Replies:
    4
    Views:
    113
Loading...

Share This Page