Re: OK in Visual C++6.0 but not in Visual C++ .NET 2003?
From: Carl Daniel [VC++ MVP] (cpdaniel_remove_this_and_nospam_at_mvps.org.nospam)
Date: 02/07/05
- Next message: Carl Daniel [VC++ MVP]: "Re: OK in Visual C++6.0 but not in Visual C++ .NET 2003?"
- Previous message: Sandy: "How to select specfic notepad file..."
- In reply to: Antti Keskinen: "Re: OK in Visual C++6.0 but not in Visual C++ .NET 2003?"
- Next in thread: Vladimir Nesterovsky: "Re: OK in Visual C++6.0 but not in Visual C++ .NET 2003?"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 7 Feb 2005 06:39:29 -0800
Antti Keskinen wrote:
> Hello !
>
> A few more questions to help me clarify certain issues. In your
> previous post, you used the syntax
>
> friend void InitClass<T>(MyClass&);
>
> to declare all InitClass<T> instantations as friends of MyClass<T>
> (so that InitClass<U> is not a friend of MyClass<T>).
The instantion of the InitClass function template with template argument <T>
is a friend of the instantiation of the MyClass template with template
arguments <T>.
>
> However, the curious thing about this comes from the parameter list.
> If I had rewritten the friend declaration inside the class as
>
> friend void InitClass<T>(MyClass<T>& refClass);
They're equivalent. Within the definition of a class template, the name of
the template without a template argument list is synonymous with the fully
specified name. i.e. MyClass and MyClass<T> are the same.
> then how would this change the situation of who's a friend and who's
> not ? Or is it just a "syntax error" situation ? Or is the <T>
> unrequired in the parameter list because we are not talking about
> template declaration, but an instantation of it, as in, the <T> gets
> "copied" into the place where the template definition specifies it
> should go ?
Exactly.
>
> Moreover, when writing the following:
>
> template<typename T>
> class MyClass;
>
> template<typename T>
> void InitClass(MyClass<T>& refClass)
> {
> }
>
> template<typename T>
> class MyClass
> {
> template<typename T> friend void InitClass(MyClass<T>& refClass);
> };
>
> Does the 'friend' keyword ensure that InitClass doesn't become some
> sort of a nested-template declaration ? So this syntax means that all
> InitClass instantations are friends of MyClass<T>, meaning, an
> InitClass<int> can access MyClass<double>'s private/protected members
> ?
Correct.
>
> Also, if using a syntax like above, is it necessary to
> forward-declare the MyClass<T> template before the function template
> ? Like, if I used the "nested-template-looking" friend declaration of
> InitClass, I could move the actual InitClass<T> definition to after
> or before the class definition, and would not need any class
> declarations ?
It's necessary to forward-declare the class, otherwise it's impossible to
specifiy the parameter list for the function since MyClass is undefined at
that point.
>
> Again, thank you for answering these questions. Like you stated,
> templates are really one of the darkest corners of C++. But, very
> rewarding as well :D
You're welcome!
-cd
- Next message: Carl Daniel [VC++ MVP]: "Re: OK in Visual C++6.0 but not in Visual C++ .NET 2003?"
- Previous message: Sandy: "How to select specfic notepad file..."
- In reply to: Antti Keskinen: "Re: OK in Visual C++6.0 but not in Visual C++ .NET 2003?"
- Next in thread: Vladimir Nesterovsky: "Re: OK in Visual C++6.0 but not in Visual C++ .NET 2003?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|