473,761 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1561
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
7872
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 impl within a class as such: a.h class A { ...stuff... struct AImpl; AImpl* _impl;
6
2188
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 the views of the experienced users of various C++ implementations? Do precompiled headers allow pimpls to be avoided while maintaining the same speed up of development time?
2
4049
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 and thus allow a clear and flexible separation of interface and implementation such that the implementation can be changed freely.
2
6114
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 declaration of pimpl class is provided. Example:
10
4498
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 between interface objects and implementation object, and the implementation object lasts for the lifetime of the interface object. i.e. // header file class WidgetImpl; class Widget {
34
3702
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 thinking of making all these pimpl class declarations public. Reasoning is that since only the code within the ..cc file will need to ever access them, why protect them in ways that would make access to them more difficult and obfuscated? What...
4
3830
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 straightforward to turn your class into a singleton object. Consider: class X { struct impl;
14
3176
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 has a nice article you can read. Anyway, I discovered that if you make all members in the implementation class mutable, you can in fact use this idiom without any "unnecessary" memory allocation. Here's a minimal example of the method: // In the...
2
2229
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 has an example but with only one implementation class: (what would an example with 2 implementation classes look like?) http://www.boost.org/doc/libs/1_35_0/libs/smart_ptr/sp_techniques.html#pimpl The pimpl'ed class cpp file has to include at...
0
9554
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9377
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10136
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8814
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6640
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3913
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.