Solved

friend of template

Posted on 2002-04-04
5
325 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
Comment Utility
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 30

Accepted Solution

by:
Zoppo earned 100 total points
Comment Utility
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
Comment Utility
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 30

Expert Comment

by:Zoppo
Comment Utility
:)

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

Author Comment

by:orenm
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now