473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1541
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.