473,411 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

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
Jul 20 '06 #1
7 1594
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
Jul 20 '06 #2
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
Jul 20 '06 #3
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
Jul 20 '06 #4
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
Jul 20 '06 #5
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
Jul 20 '06 #6
Ok, it's all becoming clear now. Thanks Victor!

Chris
Jul 21 '06 #7

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.

Jul 21 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Steve Teeples | last post by:
I don't understand why I cannot use a property to modify data within a struct. Can someone tell me why I get the error "Cannot modify the return value of "myData.TheData" because it is not a...
9
by: Macca | last post by:
Hi, I have a synchronous socket server which my app uses to read data from clients. To test this I have a simulated client that sends 100 byte packets. I have set up the socket server so...
2
by: Macca | last post by:
My app has an asynchronous socket server. It will have 20 clients connected to the server. Each client sends data every 500 millisecondsThe Connections once established will not be closed unless...
0
by: Macca | last post by:
Hi, I am writing an asychronous socket server to handle 20+ simulataneous connections. I have used the example in MSDN as a base. The code is shown at end of question. Each connection has a...
13
by: noone | last post by:
consider the following problem: You have a C style library and API that uses callbacks to implement functionality. Examples of this are X11 API, OpenGL/GLUT...The List goes on. The power of...
8
by: CedricCicada | last post by:
Greetings! Here's my script: ######## start of script class ScannerCommand: taskName = '' scanList = def __init__(self):
12
by: cfps.Christian | last post by:
I've got a nice little deadline here and have to deal with the dumbest things possible to get it done. I had to create an application that is spliced into another application via an event handler...
0
by: Tim Spens | last post by:
--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo.comwrote: I think I know where the problem is but I'm unsure how to fix it. When I call Register_Handler(...) from python via...
1
by: paragpdoke | last post by:
Hello Everyone. I am new to Windows programming / registry settings and I need help regarding setting a context menu. Allow me to provide details. - There is a DLL written in C++ which somehow is...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.