opaque pointer

This is a discussion on opaque pointer within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, Opaque pointer must be a pointer points to inner (hidden to outside) struct or class? (refer to the ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    opaque pointer

    Hello everyone,


    Opaque pointer must be a pointer points to inner (hidden to outside) struct or class?

    (refer to the C++ part)

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


    thanks in advance,
    George

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    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.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks brewbuck,


    So opaque pointers could only be pointed to inner data structures (inner structs and classes)? Can not be pointed to global/namespace level structs/classes?

    Quote Originally Posted by brewbuck View Post
    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.

    regards,
    George

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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++.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,203
    A void pointer can also be an opaque pointer: given an arbitrary void pointer, there is no way of knowing what operations (if any) are supported by the object pointed to.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats and grumpy,


    What do you think the differences between a normal member pointer and a opaque member pointer?

    Quote Originally Posted by matsp View Post
    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++.

    --
    Mats

    regards,
    George

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What do you think the differences between a normal member pointer and a opaque member pointer?
    Depending on the view you look at it: none whatsoever, or "all the difference in the world". Let me expand on that:
    No difference aspect: A pointer is a pointer is a pointer - so any pointer is the same as any other pointer [of course subject to architecture - but in most of the common architectures, any pointer can be pointing to anything it likes (subject to suitable alignment)]. Whether you know what the data structure of the content is or not doesn't change the pointer itself.

    All the difference: When you have an opaque pointer, you have no idea what the pointer is actually used for - it's just an arbitrary memory address being used for some purpose. So you can not derive any information [1] from this pointer. Which of course is the entire purpose of the opaque pointer - the interface is not wishing to inform you of what this pointer is used for or what is inside it.


    [1] Assuming we obide by the rules - there is of course no problem in most cases to cast the pointer to some arbitrary data type and then "fishing out" the content of the data structure/class. So it is only "safe" as long as the "user" is playing by the rules. It's a bit like posting "do not trespass" on a post near a field - it doesn't in itself change peoples ability to enter the field even if they are not supposed to go there - but it shows that you don't want them to.

    --
    Mats
    Last edited by matsp; 03-13-2008 at 06:41 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    I think normal pointer also does what you described below. Could you show some pseudo code about what is the special behavior of normal pointer, which is an anti-pattern of opaque pointer usage?

    BTW: in my understanding, in opaque design pattern, the opaque pointer is used for component internal usage only (e.g. containing private data structures). It is not exposed to the client of the component. Seems you mean opaque pointer could be exposed to client?

    http://www.gotw.ca/gotw/024.htm

    Quote Originally Posted by matsp View Post
    All the difference: When you have an opaque pointer, you have no idea what the pointer is actually used for - it's just an arbitrary memory address being used for some purpose. So you can not derive any information [1] from this pointer. Which of course is the entire purpose of the opaque pointer
    --
    Mats

    regards,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21