Is this legal and sensible?
class Outer
{
friend struct Inner
{
...
};
...
};
Basically I want to define the struct Outer::Inner and simultaneously
make it a friend to give it access to private details of Outer. My
compiler (gcc) accepts this, but then it also gives Inner private access
to Outer always so I can't tell if it does what I want it to do.
Thanks,
Mark 7 1942
Mark P wrote: Is this legal and sensible?
class Outer { friend struct Inner { ... };
... };
Basically I want to define the struct Outer::Inner and simultaneously make it a friend to give it access to private details of Outer. My compiler (gcc) accepts this, but then it also gives Inner private access to Outer always so I can't tell if it does what I want it to do.
More compilers grant access to private parts for nested classes these
days. Not doing so, while standard, makes no sense.
The strict way to do this would be:
class Outer
{
struct Inner;
friend struct Inner;
struct Inner {};
};
Ian
Mark P wrote in message <4_************ ***@newssvr14.n ews.prodigy.com >... Is this legal and sensible?
class Outer { friend struct Inner { ... }; ... };
Basically I want to define the struct Outer::Inner and simultaneously make it a friend to give it access to private details of Outer. My compiler (gcc) accepts this, but then it also gives Inner private access to Outer always so I can't tell if it does what I want it to do.
Thanks, Mark
Huh?!? You want to give 'Inner' private access to 'Outer', but, the compiler
gives 'Inner' private access to 'Outer'? And you want it to do what?
Try it this way:
class Outer{
public:
struct Inner; // declare it.
friend Inner; // make it a friend.
struct Inner{ // define it.
...
};
...
private:
int DuckSoup;
};
'Inner' should now have access to 'DuckSoup'.
Ref: B. Eckel 'Thinking in C++' v1, chap 5 //: C05:NestFriend. cpp
--
Bob R
POVrookie
BobR wrote: Mark P wrote in message <4_************ ***@newssvr14.n ews.prodigy.com >...
Is this legal and sensible?
class Outer { friend struct Inner { ... }; ... };
Basically I want to define the struct Outer::Inner and simultaneously make it a friend to give it access to private details of Outer. My compiler (gcc) accepts this, but then it also gives Inner private access to Outer always so I can't tell if it does what I want it to do.
Thanks, Mark
Huh?!? You want to give 'Inner' private access to 'Outer', but, the compiler gives 'Inner' private access to 'Outer'? And you want it to do what?
Sorry, I wasn't very clear. On my compiler this is a non-issue, but I
asked in the interest of portability. I know I can make a separate
friend declaration-- I just savored the idea of making my source code
one line shorter by combining this with the struct definition :)
Evidently that's not permissible though.
Mark P wrote: BobR wrote: Mark P wrote in message <4_************ ***@newssvr14.n ews.prodigy.com >...
Is this legal and sensible?
class Outer { friend struct Inner { ... }; ... };
Basically I want to define the struct Outer::Inner and simultaneously make it a friend to give it access to private details of Outer. My compiler (gcc) accepts this, but then it also gives Inner private access to Outer always so I can't tell if it does what I want it to do.
Thanks, Mark
Huh?!? You want to give 'Inner' private access to 'Outer', but, the compiler gives 'Inner' private access to 'Outer'? And you want it to do what?
Sorry, I wasn't very clear. On my compiler this is a non-issue, but I asked in the interest of portability. I know I can make a separate friend declaration-- I just savored the idea of making my source code one line shorter by combining this with the struct definition :) Evidently that's not permissible though.
To be honest, I have never been a big advocate of such constructs
(nesting or embedding classes in this way), and I'm not convinced
that I have ever seen a real need for real it.
I would much rather pass the 'Inner' object to 'Outer' via specialised
if need be, or visa versa.
Imo, this not only adds clarity, but provides a finer control of access
and security between objects.
Cheers,
Chris Val
In article <4_************ ***@newssvr14.n ews.prodigy.com >, Mark P
<no*@my.real.em ail> writes Is this legal and sensible?
class Outer { friend struct Inner { ... };
... };
Basically I want to define the struct Outer::Inner and simultaneously make it a friend to give it access to private details of Outer. My compiler (gcc) accepts this, but then it also gives Inner private access to Outer always so I can't tell if it does what I want it to do.
Thanks, Mark
There are two distinct answers here.
1) The Standard as written pulled a major boo-boo because the actual
wording does not allow granting of friendship inwards only outwards or
sideways.
class x;
class y {
friend class x; // OK
class z;
friend class z; // technical error
class a {
friend class z; // OK
// ...
};
The problem is that as written the Standard also prohibits inner classes
having access to an outer class' non-public interfaces.
This problem has now been fixed (but I cannot remember if that fix has
been published in a TC yet. It has been resolved in favour of granting
inner classes unconditional access to an enclosing class' members. That
means that the technical error above will remain an error but it will
not be necessary to write such a statement.
2) All current compilers allow the technical error above and I do not
know of any that even give a diagnostic for it. I suspect that it will
remain acceptable for the foreseeable future except when compilers are
acting in the strictest conforming mode. If you want your code to be
portable, continue to write such declarations, though increasingly they
will be unnecessary as compilers adopt the new requirement (that a
nested class be a full member of its enclosing class with full access
privileges.)
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
Chris ( Val ) wrote: Mark P wrote:
BobR wrote:
Mark P wrote in message <4_************ ***@newssvr14.n ews.prodigy.com >...
Is this legal and sensible?
class Outer { friend struct Inner { ... }; ... };
Basically I want to define the struct Outer::Inner and simultaneously make it a friend to give it access to private details of Outer. My compiler (gcc) accepts this, but then it also gives Inner private access to Outer always so I can't tell if it does what I want it to do.
Thanks, Mark
Huh?!? You want to give 'Inner' private access to 'Outer', but, the compiler gives 'Inner' private access to 'Outer'? And you want it to do what?
Sorry, I wasn't very clear. On my compiler this is a non-issue, but I asked in the interest of portability. I know I can make a separate friend declaration-- I just savored the idea of making my source code one line shorter by combining this with the struct definition :) Evidently that's not permissible though.
To be honest, I have never been a big advocate of such constructs (nesting or embedding classes in this way), and I'm not convinced that I have ever seen a real need for real it.
I would much rather pass the 'Inner' object to 'Outer' via specialised if need be, or visa versa.
Imo, this not only adds clarity, but provides a finer control of access and security between objects.
Cheers, Chris Val
I guess it's my Java heritage (where I began OOP) that made me partial
to this construct. In Java the difference is that non-static inner
classes have access to a specific instantiation of an outer class
object. This is something that I find frequently useful-- in my case,
Inner is a comparison functor with local state maintained (implicitly)
by a pointer to an Outer object. In C++, AFAIK, this relationship has
to be explicitly set up, but logically I find it sensible to group the
class definitions.
Still, I've only been at C++ for about 9 months so I'm sure my world
view is bound to change a lot more :)
Ian wrote: Mark P wrote:
Is this legal and sensible?
class Outer { friend struct Inner { ... };
... };
Basically I want to define the struct Outer::Inner and simultaneously make it a friend to give it access to private details of Outer. My compiler (gcc) accepts this, but then it also gives Inner private access to Outer always so I can't tell if it does what I want it to do. More compilers grant access to private parts for nested classes these days. Not doing so, while standard, makes no sense.
The strict way to do this would be:
class Outer { struct Inner; friend struct Inner;
struct Inner {}; };
Standards-wise: there's no legal way to grant friendship "inwards",
but with a suitably amended standard it won't be necessary; the
intent is that members (including member classes) all have access
to the private parts of a class, and if the 2003 standard doesn't
say that (I don't recall), a future version is very likely to do
so, in my opinion.
Practically speaking, for compilers that don't automatically handle
access for nested classes in this manner, the inward friend
declaration is accepted (incorrectly), and all works out fine.
-- James This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Alexander Stippler |
last post by:
Hi
what's wrong about the following code? It does not compile.
template <typename T>
class A {
public:
class B;
};
|
by: CoolPint |
last post by:
I read in books that nested class cannot access private members of
nesting class and vice versa unless they are made friends. Somehow, my
compiler is letting my nested class member functions access private
members of nesting class.
template <typename T>
class Container {
// NO friendship given to any other
public:
class ContainerIterator;
|
by: Chris Schadl |
last post by:
Okay, I'm having a bit of a brain-fart and I can't remember how I would do
this.
Say I have the following:
template <typename T1, typename T2> class A; // Forward declaration of A
template <typename T1, typename T2>
class B
{
|
by: nimmi_srivastav |
last post by:
Below you will see an example of a nested conditional expression that
this colleague of mine loves. He claims that it is more efficient that
a multi-level if-else-if structure. Moreover, our complexity analyzer
tool supposedly does not pick it up. Is it really more efficient?
Personally I find this coding style extremely cryptic, misleading and
error-prone.
I believe that I have removed all traces of proprietary-ness from this
coding...
|
by: B0nj |
last post by:
I've got a class in which I want to implement a property
that operates like an indexer, for the various colors
associated with the class.
For instance, I want to be able to do 'set' operations like
MyClass.MyColors = Color.Green
or, a 'get', such as
Color forecolor = MyClass.MyColors;
I want to use an indexer so I can take parameters, such
as the color type (e.g. "Foreground", "Background" etc.).
With a single member function I couldn't...
| |
by: Etienne Boucher |
last post by:
Nested classes are usualy objects made to only live in their parent object
instance. In other words...
public class Outter
{
public class Inner
{
}
}
|
by: Mr Dyl |
last post by:
I'm trying to declare the following friendship and VS.Net 2003 is
complaining:
template <class T>
class Outter
{
class Inner {...}
...
}
|
by: Tomas Sieger |
last post by:
Hi all,
I'm in doubt with the following code:
class Base {
public:
class Nested {};
};
class Derived:public Base {
public:
class Nested {
|
by: jdurancomas |
last post by:
Dear all,
I'm trying to declare the operator++ to a nested class. The nested
class is not template but the container it is.
The code used in teh sample program is included bellow:
#include <iostream>
|
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,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |