473,386 Members | 1,860 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,386 software developers and data experts.

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 1938
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Ellarco | last post by:
``Opaque-pointer representing the ID of an object. struct _objID; typedef struct _objID * objectID;'' Hi again. Im using an api that defines an objectID type. The above represents the extent...
19
by: Capstar | last post by:
Hi NG, I've read some time ago in this NG, that when you're writing a library it can be good practice to use an opaque type to point to your structures if the user doesn't need to modify the...
4
by: sandeep | last post by:
Hi why we cannot have static as a structure member? & also is there any way to achive data hiding in C at this level( i.e. access only selected structure member ) following code gives syntax...
18
by: chankl | last post by:
Can anyone explain what's an opaque pointer and how it's implemented in C? I read about this concept in the book "C interfaces and implementations". Here's an example from the book (list.h -...
3
by: Ernesto Bascón | last post by:
Hi everybody: I have two questions: 1. I'm using opaque pointers in my classes to hide their data structures; there is a way to use opaque pointers in template classes; since the...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
2
by: Evan Burkitt | last post by:
Hi, all. I have a Windows DLL that exports a number of functions. These functions expect to receive a pointer to a callback function and an opaque void* parameter. The callback functions are...
27
by: Nate Eldredge | last post by:
Consider the following pseudo-code: #include <opaque.h> struct foo { int a; opaque_t op; int b; };
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
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,...

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.