Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > friendship with nested class

Reply
Thread Tools

friendship with nested class

 
 
Mr Dyl
Guest
Posts: n/a
 
      11-25-2005
I'm trying to declare the following friendship and VS.Net 2003 is
complaining:


template <class T>
class Outter
{
class Inner {...}
...
}

class A
{
template <class T> friend class Outter;
template <class T> friend class Outter<T>::Inner;
}


On the second friend declaration I get the error "'Outter::Inner'
cannot be redeclared in the current scope".
Any ideas? Is this a syntax issue or does the standard actually not
allow this?

Many thanks!!

 
Reply With Quote
 
 
 
 
Sunil Varma
Guest
Posts: n/a
 
      11-25-2005
C++ compiler assumes that while accessing any member of the template
class, the class needs to be instantiated.
That's the type needs to known at compile time.

Ex.--

Try with something like.

friend class Outter<int>::Inner;

this works fine.

 
Reply With Quote
 
 
 
 
Mr Dyl
Guest
Posts: n/a
 
      11-25-2005
Sorry, I should have mentioned that this isn't possible because the
type for the template argument of Outter is unknown at this point
Technically I could forward declare every possibility and then declare
friendship with each but that's quite a hack and not terribly portable.

I was hoping there'd be some way of declaring friendship with Inner for
all template instantiations of Outter as is done with the first
friendship statement:

template <class T> friend class Outter;

Impossible????

Thx!

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      11-25-2005
* Mr Dyl:
> I'm trying to declare the following friendship and VS.Net 2003 is
> complaining:
>
>
> template <class T>
> class Outter
> {
> class Inner {...}
> ...
> }
>
> class A
> {
> template <class T> friend class Outter;
> template <class T> friend class Outter<T>::Inner;
> }
>
>
> On the second friend declaration I get the error "'Outter::Inner'
> cannot be redeclared in the current scope".
> Any ideas? Is this a syntax issue or does the standard actually not
> allow this?


Since class Inner is private you can't refer to it. However, making it
public gives errors with MSVC 7.1 and MinGW g++ 3.4.4, while compiling
fine with Comeau Online 4.3.3.

The standard allows specifying a class nested in a template class as a
friend (of a non-template class), and even gives a direct example in
paragraph 14.5.3/6. That example, with a few callable functions added:

template<class T> struct A {
struct B { void foo(); }; // 'foo' added by me
void f();
};

class C {
template<class T> friend struct A<T>::B;
template<class T> friend void A<T>::f();

static void sayNoMore() {} // 'sayNoMore' added by me
};

// Following added by me:

template<class T> void A<T>::B::foo()
{
C::sayNoMore();
}

int main()
{
A<int>::B o;
o.foo();
}

This example from the standard has the same compilation problems as your
code did.

A workaround could perhaps be to move the nested class out, and if
necessary provide a typedef in the class it's nested in.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Ed
Guest
Posts: n/a
 
      11-25-2005
You could make class A a template as well. This would allow you to
instantiate A with any type.

A<int> a1;
A<MyClass> a2
and so on.

template <typename T>
class A {
friend class Outer<T>;
friend class Outer<T>::Inner<T>;
};

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it standard that inner class inherits the outer class' friendship? Qi C++ 4 05-01-2011 04:57 AM
Friendship and nested classes Juha Nieminen C++ 2 06-13-2008 05:24 PM
Is nested class automatically friend of class that it is nested in? request@no_spam.com C++ 5 09-25-2006 08:31 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Circular Class Template Friendship Thomas Matthews C++ 7 07-28-2004 02:16 PM



Advertisments