Solved

friend of template

Posted on 2002-04-04
5
330 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Converting to C# also VS2015 5 117
C++ Language error 28 193
Arduino EDI - Programming language 3 80
Installshield for Embarcadero EX 10.1 Berlin 4 20
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

920 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

11 Experts available now in Live!

Get 1:1 Help Now