Solved

friend of template

Posted on 2002-04-04
5
351 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 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

710 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