Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

friend of template

Posted on 2002-04-04
5
Medium Priority
?
365 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

722 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