I have a class called Streamer. Here is Streamer.h
class Streamer {
public:
Streamer(const MyDb& realtimeDb);
virtual ~Streamer(void);
private:
virtual void callback_1(T_UPDATE pUpdate);
virtual void callback_2(Q_UPDATE pUpdate);
};
Here is Streamer.cpp
Streamer::Streamer(const MyDb& realtimeDb) {
}
Streamer::~Streamer(void) {
}
void Streamer::callback_1(T_UPDATE pUpdate) {
// I need to do something with pUpdate and realtimeDb here, like this:
// Getting a cursor from db (works fine in main.cpp, but not in callback)
Dbc *cursorp;
realtimeDb.getDb().cursor(NULL, &cursorp, 0);
}
void Streamer::callback_2(Q_UPDATE pUpdate) {
// I need to do something with pUpdate and realtimeDb here, like this:
// Getting a cursor from db (works fine in main.cpp, but not in callback)
Dbc *cursorp;
realtimeDb.getDb().cursor(NULL, &cursorp, 0);
}
Streamer has two methods that are callbacks from an API. I can’t change these parameters. I do, however, need to access the database instance MyDb that I am passing to the constructor (am I even doing that right?). This is how I am passing it, from main.cpp:
MyDb realtimeDb(databasePath, databaseName);
Streamer streamer(realtimeDb);
When I try to access realtimeDb from one of the callbacks, I get:
error: 'realtimeDb' was not declared in this scope
Any ideas? Thanks!
You need to create a member variable in your class to store the reference that you pass it in the constructor. Currently, you are passing in a const reference to the object but the class does nothing with it. You need to store the details of the
MyDbobject as a member variable. This could be a reference, const reference, or pointer to an instance ofMyDbbut you need something so that your class can access it once it is created.Something like
then the constructor will be
you could also use a pointer instead of a reference if you wanted although you would need to modify the member variable accordingly