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_i j. 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 7 1624
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_i j. 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(EpiCova rs& 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 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(EpiCova rs& 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
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(EpiCova rs& 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
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_i j...etc. Does this mean that I don't need the 'inline'
modifier in the class definition?
>>class EpiMath { private: EpiCovars& myData;
public: EpiMath(cons t 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(EpiCova rs& 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 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_i j...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(con st 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(EpiCova rs& 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
Ok, it's all becoming clear now. Thanks Victor!
Chris
Chris Jewell wrote:
class EpiMath
{
private:
EpiCovars& myData;
public:
EpiMath(const EpiCovars& dataRef) {
myData = dataRef;
}
Change to:
EpiMath(EpiCova rs & 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 variable.
Here is what breaks.
struct Data
{
public Data(){}
private int someData;
|
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 that its buffer is bigger than this.
I did this expecting the data to be read in one pass.
|
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 there is a problem with the
connection.
I need to know which client has sent the incoming data as each client has
its own buffer on my "server" app.
I am using the standard asynch socket code from MSDN to listen for
connections and they...
|
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 number of different types of data coming in. I have a
databuffer for each type of data coming in.
|
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 virtuals in C++ leads us to want to implement a framework
where those callbacks are simply overriden virtual methods in a derived
class. So...
| |
by: CedricCicada |
last post by:
Greetings!
Here's my script:
######## start of script
class ScannerCommand:
taskName = ''
scanList =
def __init__(self):
|
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 class. When
the original program sees the event handler is uses the handler's code
to execute my code (I code the handler as well). Anyway I've coded
all this and of course it works locally but I don't need it to work
locally I need it to work...
|
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 callback.setHandler1(callback1) this only seems to affect pythons ability to trigger an "event" in c. PyObject *Handler is always NULL even after I call Register_Handler(...). I thought there was some magic here that was assigning the pointer *Handler to my python...
|
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 able to display a "MyApp" menu when I right click any file. I am not sure how it displays the menu (not owned by me).
- The programming language I used to register the content menu handler is VB.NET. I have code that creates a new key:
...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |