Having trouble with class scope
-
26-06-2021 - |
Question
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!
Solution
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 MyDb
object as a member variable. This could be a reference, const reference, or pointer to an instance of MyDb
but you need something so that your class can access it once it is created.
Something like
class Streamer {
public:
Streamer(const MyDb& Db);
virtual ~Streamer(void);
private:
const MyDb& realtimeDb;
virtual void callback_1(T_UPDATE pUpdate);
virtual void callback_2(Q_UPDATE pUpdate);
};
then the constructor will be
Streamer::Streamer(const MyDb& Db)
: realtimeDb(Db) // initialise the reference here
{
}
you could also use a pointer instead of a reference if you wanted although you would need to modify the member variable accordingly
OTHER TIPS
This isn't really "class scope" you have, but constructor scope.
Streamer::Streamer(const MyDb& realtimeDb)
{
//realtimeDb exists only here
}
You can do something like this:
streamer.h
class Streamer {
public:
Streamer(const MyDb& realtimeDb);
virtual ~Streamer(void);
private:
MyDb* realtimeDb;
virtual void callback_1(T_UPDATE pUpdate);
virtual void callback_2(Q_UPDATE pUpdate);
};
streamer.cpp
Streamer::Streamer(const MyDb& realtimeDb) {
this->realtimeDb = &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);
}
This way realtimeDb pointer will exist everywhere in the class scope, so every non-static method can access it.
But, because it's a pointer, syntax will be different.
Also, be careful - if someone declares Streamer with default constructor, realtimeDb will be NULL, and callbacks will invoke undefined behaviour.
Your realtimeDb is defined as an argument of your constructor, but it's not defined in the callbacks.
What you probably want to do is to keep the reference to that object, like this:
class Streamer {
public:
Streamer(MyDb& realtimeDb); // I doubt you need the "const"...
virtual ~Streamer(void);
private:
virtual void callback_1(T_UPDATE pUpdate);
virtual void callback_2(Q_UPDATE pUpdate);
MyDb& m_realtimeDb;
};
And your implementation:
Streamer::Streamer(const MyDb& realtimeDb) : m_realtimeDb(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;
m_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;
m_realtimeDb.getDb().cursor(NULL, &cursorp, 0);
}