-
July 22nd, 2011, 07:13 PM
#1
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.
-
July 22nd, 2011, 11:38 PM
#2
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.
-
July 23rd, 2011, 03:03 AM
#3
Re: extern a public member of a class - is it possible?
I realized that after I posted, thanks for the reply though
-
July 23rd, 2011, 12:23 PM
#4
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.
-
July 24th, 2011, 01:57 PM
#5
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?
-
July 24th, 2011, 02:25 PM
#6
Re: extern a public member of a class - is it possible?
Originally Posted by drwbns
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.
-
July 24th, 2011, 03:04 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|