473,395 Members | 1,377 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,395 software developers and data experts.

nested friend syntax?

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
Jul 23 '05 #1
7 1925
Ian
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
Jul 23 '05 #2

Mark P wrote in message <4_***************@newssvr14.news.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
Jul 23 '05 #3
BobR wrote:
Mark P wrote in message <4_***************@newssvr14.news.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.
Jul 23 '05 #4


Mark P wrote:
BobR wrote:
Mark P wrote in message <4_***************@newssvr14.news.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

Jul 23 '05 #5
In article <4_***************@newssvr14.news.prodigy.com>, Mark P
<no*@my.real.email> 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
Jul 23 '05 #6
Chris ( Val ) wrote:

Mark P wrote:
BobR wrote:
Mark P wrote in message <4_***************@newssvr14.news.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 :)
Jul 23 '05 #7
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
Jul 23 '05 #8

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

Similar topics

1
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; };
8
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...
1
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...
10
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...
6
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...
8
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 { } }
4
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 {...} ... }
1
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 {
3
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: ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.