472,986 Members | 2,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,986 software developers and data experts.

Pimpl idiom in MC++ classes

Because 'friend' is not recognized in MC++, using the pImpl idiom in MC++
classes seems nearly impossible. Normally a pImpl class is a 'friend' to the
class for which it supplies the private implementation, so that it can
access any protected members, including inherited protected members, of that
class. Without 'friend' the pImpl class can no longer do this, and it is a
PITA passing the necessary protected data or protected member function
pointers to the pImpl idiom member functions each time it may need it.

Is there a good workaround for this in MC++ ?
Nov 16 '05 #1
9 1524
Edward Diener wrote:
Because 'friend' is not recognized in MC++, using the pImpl idiom in MC++
classes seems nearly impossible. Normally a pImpl class is a 'friend' to the
class for which it supplies the private implementation, so that it can
access any protected members, including inherited protected members, of that
class. Without 'friend' the pImpl class can no longer do this, and it is a
PITA passing the necessary protected data or protected member function
pointers to the pImpl idiom member functions each time it may need it.

Is there a good workaround for this in MC++ ?


It seems like most of my pImpl classes don't need to be friends of their
"host" classes, but in any event, it shouldn't be necessary to make the
pImpl class a friend for it to have full access to the host class. ISTR that
VC7 tracks this DR:

http://www.comeaucomputing.com/iso/cwg_defects.html#45

So the following should work:

class X
{
private:

class Y;
Y* y;

struct Z {};

void g(Z&);

public:

void f();
};

class X::Y
{
private:

struct D : X::Z {};

public:

void f(X& x)
{
X::Z z;
x.g(z);
}
};

void X::f()
{
y->f(*this);
}

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 16 '05 #2
Doug Harrison [MVP] wrote:
So the following should work:

class X
{
private:

class Y;
Y* y;

struct Z {};

void g(Z&);

public:

void f();
};

class X::Y
{
private:

struct D : X::Z {};

public:

void f(X& x)
{
X::Z z;
x.g(z);
}
};

void X::f()
{
y->f(*this);
}


Sorry, that's an example for regular C++ classes, not __gc classes. To get
the latter, add the necessary __gc's and fix the declaration and usage of
'z' in X::Y::f.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 16 '05 #3
Doug Harrison [MVP] wrote:
Doug Harrison [MVP] wrote:
So the following should work:

class X
{
private:

class Y;
Y* y;

struct Z {};

void g(Z&);

public:

void f();
};

class X::Y
{
private:

struct D : X::Z {};
I don't think this should work. X does not have access to X::Z which is
private to X

public:

void f(X& x)
{
X::Z z;
x.g(z);
Ditto. No access to X::Z or x.g, both of which are private.
}
};

void X::f()
{
y->f(*this);
}


Sorry, that's an example for regular C++ classes, not __gc classes.
To get the latter, add the necessary __gc's and fix the declaration
and usage of 'z' in X::Y::f.


I had never used pImpl as a nested class, but rather as a separate class
which is a friend to its host class. But even in the nested class situation,
a nested class does not have access to the private or protected members of
its surrounding class. Unless the rules have changed drastically somehow.
Nov 16 '05 #4
Edward Diener wrote:
I had never used pImpl as a nested class, but rather as a separate class
which is a friend to its host class.
I don't think I've ever used anything but a private nested class for this,
one which I declare in the header and complete in the .cpp file, so that
it's truly hidden from users of the host class.
But even in the nested class situation,
a nested class does not have access to the private or protected members of
its surrounding class. Unless the rules have changed drastically somehow.


The rules which didn't give access to nested or local classes were never
very helpful. Like I said in my first reply:

It seems like most of my pImpl classes don't need to be friends of their
"host" classes, but in any event, it shouldn't be necessary to make the
pImpl class a friend for it to have full access to the host class. ISTR that
VC7 tracks this DR:

http://www.comeaucomputing.com/iso/cwg_defects.html#45

Try the example I gave you. It compiles with VC 7.1 and Comeau, but not VC6.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 16 '05 #5
Doug Harrison [MVP] wrote:
Edward Diener wrote:
But even in the nested class situation,
a nested class does not have access to the private or protected
members of its surrounding class. Unless the rules have changed
drastically somehow.


The rules which didn't give access to nested or local classes were
never very helpful. Like I said in my first reply:

It seems like most of my pImpl classes don't need to be friends of
their "host" classes, but in any event, it shouldn't be necessary to
make the pImpl class a friend for it to have full access to the host
class. ISTR that VC7 tracks this DR:

http://www.comeaucomputing.com/iso/cwg_defects.html#45

Try the example I gave you. It compiles with VC 7.1 and Comeau, but
not VC6.


I am trying to understand why it compiles. Have the rules regarding access
by members of a nested class to the protected and private members and types
of its enclosing class changed ? Because that is clearly what you are doing
in the example. So when it compiles successfully in VC7.1 and Comeau, it
appears that neither are following the C++ Standard. Surely there is
something I am missing here. Have the rules changed for these compilers from
the 1998 C++ Standard ?
Nov 16 '05 #6
Edward Diener wrote:
Doug Harrison [MVP] wrote:
Try the example I gave you. It compiles with VC 7.1 and Comeau, but
not VC6.


I am trying to understand why it compiles. Have the rules regarding
access by members of a nested class to the protected and private
members and types of its enclosing class changed ? Because that is
clearly what you are doing in the example. So when it compiles
successfully in VC7.1 and Comeau, it appears that neither are
following the C++ Standard. Surely there is something I am missing
here. Have the rules changed for these compilers from the 1998 C++
Standard ?


Did you read the text of DR #45? The rules _have_ radically changed, or
would under the proposed resolution which says "A nested class is a member
and as such has the same access rights as any other member.". VC and Comeau
both chose to implement the proposed rule change even though the DR is not
part of the official standard yet.

-cd

Nov 16 '05 #7
Edward Diener wrote:
Doug Harrison [MVP] wrote:
Edward Diener wrote:
But even in the nested class situation,
a nested class does not have access to the private or protected
members of its surrounding class. Unless the rules have changed
drastically somehow.


The rules which didn't give access to nested or local classes were
never very helpful. Like I said in my first reply:

It seems like most of my pImpl classes don't need to be friends of
their "host" classes, but in any event, it shouldn't be necessary to
make the pImpl class a friend for it to have full access to the host
class. ISTR that VC7 tracks this DR:

http://www.comeaucomputing.com/iso/cwg_defects.html#45

Try the example I gave you. It compiles with VC 7.1 and Comeau, but
not VC6.


I am trying to understand why it compiles. Have the rules regarding access
by members of a nested class to the protected and private members and types
of its enclosing class changed ? Because that is clearly what you are doing
in the example. So when it compiles successfully in VC7.1 and Comeau, it
appears that neither are following the C++ Standard. Surely there is
something I am missing here. Have the rules changed for these compilers from
the 1998 C++ Standard ?


The code I presented is illegal by the original rules, but legal under the
resolution of the defect report I pointed you to. This DR has "WP" status,
which means:

"WP: A DR issue that the Committee has voted to apply to the current Working
Paper. The Working Paper is a draft for a future version of the Standard."

To "track this DR" is to implement the changes it proposes.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 16 '05 #8
Carl Daniel [VC++ MVP] wrote:
Edward Diener wrote:
Doug Harrison [MVP] wrote:
Try the example I gave you. It compiles with VC 7.1 and Comeau, but
not VC6.


I am trying to understand why it compiles. Have the rules regarding
access by members of a nested class to the protected and private
members and types of its enclosing class changed ? Because that is
clearly what you are doing in the example. So when it compiles
successfully in VC7.1 and Comeau, it appears that neither are
following the C++ Standard. Surely there is something I am missing
here. Have the rules changed for these compilers from the 1998 C++
Standard ?


Did you read the text of DR #45? The rules _have_ radically
changed, or would under the proposed resolution which says "A nested
class is a member and as such has the same access rights as any other
member.". VC and Comeau both chose to implement the proposed rule
change even though the DR is not part of the official standard yet.


Ah, now I understand. That is a pretty drastic change to C++. I am a bit
surprised it was made, although I can understand its usefulness. I obviously
didn't understand the full impact of DR #45 when I read it.
Nov 16 '05 #9
Doug Harrison [MVP] wrote:
Edward Diener wrote:
Doug Harrison [MVP] wrote:
Edward Diener wrote:
But even in the nested class situation,
a nested class does not have access to the private or protected
members of its surrounding class. Unless the rules have changed
drastically somehow.

The rules which didn't give access to nested or local classes were
never very helpful. Like I said in my first reply:

It seems like most of my pImpl classes don't need to be friends of
their "host" classes, but in any event, it shouldn't be necessary to
make the pImpl class a friend for it to have full access to the host
class. ISTR that VC7 tracks this DR:

http://www.comeaucomputing.com/iso/cwg_defects.html#45

Try the example I gave you. It compiles with VC 7.1 and Comeau, but
not VC6.


I am trying to understand why it compiles. Have the rules regarding
access by members of a nested class to the protected and private
members and types of its enclosing class changed ? Because that is
clearly what you are doing in the example. So when it compiles
successfully in VC7.1 and Comeau, it appears that neither are
following the C++ Standard. Surely there is something I am missing
here. Have the rules changed for these compilers from the 1998 C++
Standard ?


The code I presented is illegal by the original rules, but legal
under the resolution of the defect report I pointed you to. This DR
has "WP" status, which means:

"WP: A DR issue that the Committee has voted to apply to the current
Working Paper. The Working Paper is a draft for a future version of
the Standard."

To "track this DR" is to implement the changes it proposes.


Ok, now I realize the full impact of the DR and what is meant by tracking
it. That is a big change to C++. I never realized that this had been
proposed and accepted in the Working Paper. Thanks !
Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Icosahedron | last post by:
I've been going through some old code trying to clean it up and rearchitect it based on more modern C++ idioms. In the old code I often used the Pimpl idiom on a class by class basis, creating...
6
by: Asfand Yar Qazi | last post by:
Hi, Now that GCC 3.4 has precompiled headers, I'm thinking I can stop using pimpls to speed up development time, as it may make life easier (declaring pimpls takes a long time...) What are...
2
by: Debajit Adhikary | last post by:
I'm still pretty new to design patterns... I was wondering, is there any difference between the Bridge Pattern and Herb Sutter's Pimpl Idiom? Both delegate responsibility to an implementation...
2
by: Peteris Krumins | last post by:
Hello! I was playing around pimpl idiom and discovered that there is a problem with it if a class member template function exists which has to access private data, since only the forward...
10
by: red floyd | last post by:
It seems that the use of auto_ptr<> is discouraged in many places in favor of boost::shared_ptr<> (or tr1::shared_ptr<>). But consider a PIMPL idiom, where there is a strict 1-1 relationship...
34
by: Asfand Yar Qazi | last post by:
Hi, I'm creating a library where several classes are intertwined rather tightly. I'm thinking of making them all use pimpls, so that these circular dependancies can be avoided easily, and I'm...
4
by: Noah Roberts | last post by:
Some little tidbit I just ran into that might help some, especially novice programmers. If you are using the pimpl idiom, as you probably should be most of the time, then it is very...
14
by: Daniel Lidström | last post by:
Hello! I have just discovered a way to use the private implementation idiom (pimpl), without the overhead of dynamic memory allocation. For those of you who don't know what this is, Wikipedia...
2
by: Graham Reitz | last post by:
What are good strategies for selecting, either at run-time or compile time, various pimpl'ed implementations? While retaining the ability to switch implementations without recompiling. Boost...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...

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.