Solved

friend of template

Posted on 2002-04-04
5
342 Views
Last Modified: 2010-04-02
Hi Expert,

template <class T>
class C
{
friend class T;

private:
     void f();
}

Need class T be the friend of the class C template.
This declaration is NOT accepted, class T can not access private members of C.
Do you know how this should be declared ?

TIA,
Oren.
0
Comment
Question by:orenm
  • 2
  • 2
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6917992
Are you using VC++? If so, see http://support.microsoft.com/default.aspx?scid=kb;EN-US;q168384 ("PRB: C2248: No Access for Friend Class in a Template Class (Q168384)")
0
 
LVL 31

Accepted Solution

by:
Zoppo earned 100 total points
ID: 6918273
Hmm, for strange I got a sample compiling/working ok on VC++:

template <class T>
class A
{
 friend T; // the same without the 'class'
 void f() { cout << "In A::f()." << endl; }
};

class B
{
public:
 void test(){
  A <B> a;// no compile error; OK
  a.f();
 }
};

class C
{
public:
 void test() {
  A <B> a;// compile error as expected
  a.f();
 }
};

Please don't ask me why this works this way...

ZOPPO
0
 
LVL 86

Expert Comment

by:jkr
ID: 6918296
Zoppo, you found a VC++ bug :o)

See the article above: "This behavior is by design as per C++ standard section 7.1.5.3 paragraph 2"
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 6918306
:)

Again such a 'bug or feature'-bug...
0
 

Author Comment

by:orenm
ID: 6920337
Hi,

This is probably not a bug,
this syntax is also make some sense,
because such a friend can only be a class.

Thanx any way,

Oren.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question