close

Opaque pointer

wiki

http://en.wikipedia.org/wiki/Opaque_pointer

 

http://bytes.com/topic/c/answers/524588-opaque-pointers

http://cboard.cprogramming.com/cplusplus-programming/100298-opaque-pointer.html

 

An opaque pointer is just a pointer to an incomplete type. I disagree with the Wikipedia page. The Pimpl idiom is not about "opaque pointers," it's about providing a thin layer which allows you to hide the private implementation. But the wrapper itself isn't opaque -- otherwise you couldn't do anything at all with the pointer.

An opaque pointer is used when you want to give an object to somebody but you don't want them to have the slightest clue what it is. That's not what Pimpl is about. Pimpl is about hiding the implementation. Opacity hides the implementation AND the interface.

Opaque pointers are used a lot in C, too.

 

An opaque pointer can point to any type. This is an example of opaque pointer:

Code:
// api.h
struct context;

context *createcontext(...);


// api.cpp
struct context 
{
....
}

context *createcontext(...)
{
   context *ctxt = new context;
...
   return ctxt;
}


// appl.cpp
#include "api.h"

int main()
{
   struct context *ctxt;

   ctxt = createcontext(...);
   ...
   return 0;
}

The point is that the context structure is unknown in the application (appl.cpp), but it is known inside the api.c

The above code could just as well be C as C++.

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 阿肯 的頭像
    阿肯

    韌體開發筆記

    阿肯 發表在 痞客邦 留言(0) 人氣()