468,532 Members | 1,782 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,532 developers. It's quick & easy.

static opaque pointers?

Hello to everybody,
I'm trying to write a small program which is meant to interact with an
sqlite database. My idea is to create once a database handler, which
happens to be an opaque pointer (a pointer to a struct which is not
defined anywhere, if I understood correctly). Now, if I had the
possibility of instantiating a concrete struct, I would create a
function like this:

struct sqlite& myDb()
{
static struct sqlite mydb;
return mydb;
}

And then use myDb() whenever the uniquely created object is needed, and
&myDb() whenever the pointer to my database is needed. My moderate
knowledge of C++ does not help me here. I cannot create a concrete
struct sqlite, everything in the API uses an opaque pointer (and
pointers to this pointer). How can I modify the above code and get a
static opaque pointer? I'm actually wondering if it is at all possible.
If not, what do people do in these situations? Is there any widely
accepted/used idiom?
Thank you in advance for any advice,

Alberto

Feb 5 '06 #1
2 1706
jashugun wrote:
I'm trying to write a small program which is meant to interact with an
sqlite database. My idea is to create once a database handler, which
happens to be an opaque pointer (a pointer to a struct which is not
defined anywhere, if I understood correctly). Now, if I had the
possibility of instantiating a concrete struct, I would create a
function like this:

struct sqlite& myDb()
{
static struct sqlite mydb;
return mydb;
}

And then use myDb() whenever the uniquely created object is needed,
and &myDb() whenever the pointer to my database is needed. My moderate
knowledge of C++ does not help me here. I cannot create a concrete
struct sqlite, everything in the API uses an opaque pointer (and
pointers to this pointer). How can I modify the above code and get a
static opaque pointer? I'm actually wondering if it is at all
possible. If not, what do people do in these situations? Is there any
widely accepted/used idiom?


Well, I generally use an abstract base class for that. You define
a class that has all the interface just like 'mydb' would, and then
declare all members virtual. Then you derive your concrete class from
it and define the behaviour is all the overriders. Something like

// user includes this header:
struct sqlite {
virtual bool do_that();
virtual void do_something_else();
};

sqlite& myDb();

// and you put the rest in a library:
#include <sqlite.h>
struct sqlite_impl : sqlite {
bool do_that() {
// something specific here...
return false; // or whatever
}
void do_something_else() {
// something else
}
};
sqlite& myDb() {
static sqlite_impl actual_thing;
return actual_thing;
}

In this case a pointer (or a reference) is not opaque in terms of what
it can do, but it's opaque in _how_ it does it.

Another possibility (which is essentially the same thing), is to have
all the functions in your library be non-members and accept the pointer
to your 'sqlite', which internally will be used polymorphically, for
example. That's just a functional wrapper around the class, allowing
change the actual class without the user's code changes. However, it
would still require rewriting the wrapper to accommodate class changes.

V
--
Please remove capital As from my address when replying by mail
Feb 5 '06 #2
Thank you very much for the suggestion.
Best,

Alberto

Feb 5 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Ellarco | last post: by
19 posts views Thread by Capstar | last post: by
18 posts views Thread by chankl | last post: by
3 posts views Thread by Ernesto Bascón | last post: by
27 posts views Thread by Nate Eldredge | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.