CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2011
    Posts
    144

    extern a public member of a class - is it possible?

    Hi guys, I was trying to access an object's member I've created in one file, from another file. The problem I think is that the member is a Private Implementation for that class. I've tried to "extern" the member with no luck. So how can I get the object from one class to another? I hope I made this clear enough...


    To make things clearer -

    //MyClass.h
    Code:
    class MyClass {
    public:
    class PImpl;
    PImpl *p;
    }
    //MyClass.cpp
    Code:
    class MyClass::PImpl {
         public:
           SomeClass * member;
    }
    
    MyClass::MyClass {
    p = new PImpl;
    p->member = SomeFunction();
    }
    and I need to access p in another header file, how?
    Last edited by drwbns; July 22nd, 2011 at 07:25 PM.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: extern a public member of a class - is it possible?

    p is a public member, so as long as you have a MyClass object, you can access its p member.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Mar 2011
    Posts
    144

    Re: extern a public member of a class - is it possible?

    I realized that after I posted, thanks for the reply though

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: extern a public member of a class - is it possible?

    In general, accessing p directly is in contradiction to the pimpl design pattern. Anything within PImpl which you need to access externally should be passed through a method in MyClass.

  5. #5
    Join Date
    Mar 2011
    Posts
    144

    Re: extern a public member of a class - is it possible?

    when I try to access p such as with -

    MyClass *m;

    then try to access the member using -

    m->p

    I get the error pointer to incomplete class type is not allowed. I also tried to add a getPImpl() function that returns p to Myclass, and that won't return p for some reason. Why?

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: extern a public member of a class - is it possible?

    Quote Originally Posted by drwbns View Post
    when I try to access p such as with -

    MyClass *m;

    then try to access the member using -

    m->p

    I get the error pointer to incomplete class type is not allowed.
    In whatever context you're using m->p, the compiler needs to know the full definition of class Pimpl. In other words, if the compiler doesn't know what a Pimpl is, you can't access members of unknown types. Forward declarations, as you did in your first post, is only good for pointer and reference declarations, and maybe a scant few other scenarios. So in general, usage of such forward-declared types requires the full definition to be available.

    So in your CPP file, just include "pimpl.h", or whatever you called the file that has class Pimpl defined.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 24th, 2011 at 02:33 PM.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: extern a public member of a class - is it possible?

    How to make it work is beside the point. The primary thing to understand is-----if you need access to p, then the pimpl design pattern is not appropriate. You are not using it correctly if p is exposed anywhere except in the MyClass methods.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured