472,954 Members | 1,994 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 software developers and data experts.

using friend and template class

Hi,

is a construction like the following possible:

template<class view_model>
class template_clase
{
protected:
template_clase() {}
virtual ~template_clase() {}
};

clase friend_class
{
friend_class() {}
virtual ~friend_class{}

friend template_class;
};

My compile ( Visual C++ 7.0 ) doesn't accept this code. Is it possible and
if yes how, to use a template class as a friend class?

Thanks in advance
Sebastian
Jul 19 '05 #1
21 8489
WW
Sebastian Faust wrote:
Hi,

is a construction like the following possible:

template<class view_model>
class template_clase
{
protected:
template_clase() {}
virtual ~template_clase() {}
};

clase friend_class
{
friend_class() {}
virtual ~friend_class{}

friend template_class;
};

My compile ( Visual C++ 7.0 ) doesn't accept this code. Is it
possible and if yes how, to use a template class as a friend class?


There is no such thing as template class. There are class templates. Read
this two sentences until you grasp the difference. Class templates are
*only* templates. They are not classes. So they cannot be made friends.

Furthermore I do not understand that why do you call the class giving friend
access friend_class? In C++ we call friend class the class *being given*
the friendship. Furthermore you have template_clase and template_class. I
assume this is a typo.

--
WW aka Attila
Jul 19 '05 #2
Hi,

This code is works (VC++ 7.1):
template<class view_model> class template_clase

{

protected:

template_clase() {}

virtual ~template_clase() {}

};

class friend_class

{

friend_class() {}

virtual ~friend_class() {}

friend class template_clase<class view_model>; // this is a
template!!!!

};

:-))

Have fun,

Viatcheslav.

"Sebastian Faust" <sf*************@logic-software.de> wrote in message
news:bl*************@news.t-online.com...
Hi,

is a construction like the following possible:

template<class view_model>
class template_clase
{
protected:
template_clase() {}
virtual ~template_clase() {}
};

clase friend_class
{
friend_class() {}
virtual ~friend_class{}

friend template_class;
};

My compile ( Visual C++ 7.0 ) doesn't accept this code. Is it possible and
if yes how, to use a template class as a friend class?

Thanks in advance
Sebastian

Jul 19 '05 #3

"WW" <wo***@freemail.hu> wrote in message
news:bl**********@phys-news1.kolumbus.fi...
Sebastian Faust wrote:
Hi,

is a construction like the following possible:

template<class view_model>
class template_clase
{
protected:
template_clase() {}
virtual ~template_clase() {}
};

clase friend_class
{
friend_class() {}
virtual ~friend_class{}

friend template_class;
};

My compile ( Visual C++ 7.0 ) doesn't accept this code. Is it
possible and if yes how, to use a template class as a friend class?
There is no such thing as template class. There are class templates.

Read this two sentences until you grasp the difference. Class templates are
*only* templates. They are not classes. So they cannot be made friends.
1. At least at definition we call it "template <typename X> class Y". :-))
2. Both "template classes" and "classes" are data abstractions.
3. Both abstractions has visibility rules (public, protected, private) for
data/methods.
4. For some other abstractions (does not matter - "template classes",
"classes", "functions") we can allow to neglect visibility rules. (This is
what "friend"(s) are for :-)) )

5. Therefore: there is no logical reason that template classes can not made
friends.

Viatcheslav.

Furthermore I do not understand that why do you call the class giving friend access friend_class? In C++ we call friend class the class *being given*
the friendship. Furthermore you have template_clase and template_class. I assume this is a typo.

--
WW aka Attila

Jul 19 '05 #4
WW
Viatcheslav L. Gorelenkov wrote:
"WW" <wo***@freemail.hu> wrote in message
There is no such thing as template class. There are class
templates. Read this two sentences until you grasp the difference.
Class templates are *only* templates. They are not classes. So
they cannot be made friends.
1. At least at definition we call it "template <typename X> class Y".
:-))


Haha
2. Both "template classes" and "classes" are data abstractions.
There is no such thing as template class.
3. Both abstractions has visibility rules (public, protected,
private) for data/methods.
Class templates has none. Class template instances (which are classes)
have.
4. For some other abstractions (does not matter - "template classes",
"classes", "functions") we can allow to neglect visibility rules.
(This is what "friend"(s) are for :-)) )
There is no such thing as template class.
5. Therefore: there is no logical reason that template classes can
not made friends.

Except that there is no such thing as template class.

Only class templates exist.

--
WW aka Attila
Jul 19 '05 #5
WW
Viatcheslav L. Gorelenkov wrote:
[SNIP]
friend class template_clase<class view_model>; // this is a
template!!!!


Wrong again. The above is *not* a template. It is a concrete type, a
concrete instance of a class template. For newbies: this is a beautiful way
of how *not* to learn anything. With trial and error, not even knowing what
a class template is, trying to insert and remove things until it works - and
finally still *not knowing anything* and state that a concrete class is a
template.

If you ever decide to learn anything do it right. Learn what the
terminology is, because that defines already a lot. And if you did not
learn it and you are warned about using the worng word(s) here: think about
it. Unless you want to make a fool of yourself as the above person did.

--
WW aka Attila
Jul 19 '05 #6

"WW" <wo***@freemail.hu> wrote in message
news:bl**********@phys-news1.kolumbus.fi...
Viatcheslav L. Gorelenkov wrote:
[SNIP]
friend class template_clase<class view_model>; // this is a
template!!!!
Wrong again. The above is *not* a template. It is a concrete type, a
concrete instance of a class template. For newbies: this is a beautiful

way of how *not* to learn anything. With trial and error, not even knowing what a class template is, trying to insert and remove things until it works - and finally still *not knowing anything* and state that a concrete class is a
template.
1. The above IS template. This is NOT a concreate instance. Take a look what
is inside angle brackets ( <..> ).
If you ever decide to learn anything do it right.
2. Completely agree.

Viatcheslav.
Learn what the
terminology is, because that defines already a lot. And if you did not
learn it and you are warned about using the worng word(s) here: think about it. Unless you want to make a fool of yourself as the above person did.

--
WW aka Attila

Jul 19 '05 #7
WW
Viatcheslav L. Gorelenkov wrote:
"WW" <wo***@freemail.hu> wrote in message
news:bl**********@phys-news1.kolumbus.fi...
Viatcheslav L. Gorelenkov wrote:
[SNIP]
friend class template_clase<class view_model>; // this is a
template!!!!


Wrong again. The above is *not* a template. It is a concrete type,
a concrete instance of a class template. For newbies: this is a
beautiful way of how *not* to learn anything. With trial and error,
not even knowing what a class template is, trying to insert and
remove things until it works - and finally still *not knowing
anything* and state that a concrete class is a template.

1. The above IS template. This is NOT a concreate instance. Take a
look what is inside angle brackets ( <..> ).


I have missed the class keyword there. Blame it on my decomposing mind.
Unfortunately the aboveis not yet C++, but it is proposed!

I thought you wanted to make one *instance* of that template a friend, the
one instantiated from friend_class. I guess I have to work harder on
eliminating my preconceptions when reading a post. And especially when
answering to it. :-(
If you ever decide to learn anything do it right.


2. Completely agree.


The "template friend" you are looking for it (right now) not part of C++.
AFAIK there is a proposal to add it - but I cannot find it.

--
WW aka Attila
Jul 19 '05 #8
"Sebastian Faust" <sf*************@logic-software.de> wrote in message news:<bl*************@news.t-online.com>...
template<class view_model>
class template_clase
{
protected:
template_clase() {}
virtual ~template_clase() {}
};

clase friend_class
{
friend_class() {}
virtual ~friend_class{}

WRONG:
friend template_class;
RIGHT:

template <class> friend class template_class;

It's a template, so tell that to the compiler. Also, the keyword
"class" is required after "friend" if it's a class or class template.
};

My compile ( Visual C++ 7.0 ) doesn't accept this code. Is it possible and
if yes how, to use a template class as a friend class?


Visual C++ 7.0 does allow the above code. (6.0, however, does not; it
does not allow template friends at all.)

- Shane
Jul 19 '05 #9
WW
WW wrote:
[SNIP my junk]

I have to sleep more. Or less. I have been lost in the timeline, back to
1995. Of course you can make all instances of a class template friend. See
Shane's post.

--
WW aka Attila
Shaving hair off again, preparing ash
Jul 19 '05 #10
"Viatcheslav L. Gorelenkov" <vl****@boo.net> wrote in message news:<bl**********@news.chatlink.com>...
friend class template_clase<class view_model>; // this is a
template!!!!


Nope. You're giving friendship to template_clase<view_model>, where
view_model is a class, not template_clase itself. This is similar to
the following:

template <typename T> class foo {
public:
typedef typename T::type type;
};

class bar {
private:
typedef void type;
// give friendship to foo<bar>, where bar is a class
friend class foo<bar>;
};

// explicitly instantiate foo<bar> (for testing purposes)
template class foo<bar>;

Only foo<bar> is friends with foo, not foo<int>, etc. If you want all
instantiations of foo to be given friendship, do as thus:

template <class> friend class foo;

This makes friends with foo, which is a class template which takes one
parameter.

- Shane
Jul 19 '05 #11
WW
Shane Beasley wrote:
[SNIP]
template <class> friend class foo;

This makes friends with foo, which is a class template which takes one
parameter.


Yep. (Hopefully) temporary insanity from my side.

--
WW aka Attila
Jul 19 '05 #12

"Shane Beasley" <sb******@cs.uic.edu> wrote in message
news:2f**************************@posting.google.c om...
"Viatcheslav L. Gorelenkov" <vl****@boo.net> wrote in message news:<bl**********@news.chatlink.com>...
friend class template_clase<class view_model>; // this is a
template!!!!


1. This is a template. ( !!!!! :-))) ) See source code in my 1-st posting. I
do not give friendship to instantiated template_clase<view_model>, rather it
is friendship to class template_clase<class view_model>. See what is between
the angle brackets <...>. Your can change view_model to C, T, whatever - it
still will compile and work the same way.

Nope. You're giving friendship to template_clase<view_model>, where
view_model is a class, not template_clase itself. This is similar to
the following:

template <typename T> class foo {
public:
typedef typename T::type type;
};

class bar {
private:
typedef void type;
// give friendship to foo<bar>, where bar is a class
friend class foo<bar>;
2. To give friendship to template class foo :
friend class foo<class TTT>; // CLASS TTT

This is true for MS VC++ compiler ( see original posting).

Viatcheslav.

};

// explicitly instantiate foo<bar> (for testing purposes)
template class foo<bar>;

Only foo<bar> is friends with foo, not foo<int>, etc. If you want all
instantiations of foo to be given friendship, do as thus:

template <class> friend class foo;

This makes friends with foo, which is a class template which takes one
parameter.

- Shane

Jul 19 '05 #13
"Viatcheslav L. Gorelenkov" <vl****@boo.net> wrote in message news:<bl**********@news.chatlink.com>...
friend class template_clase<class view_model>; // this is a
template!!!!


1. This is a template. ( !!!!! :-))) ) See source code in my 1-st posting.


I thought the above code was yours... Did I copy it incorrectly?
I
do not give friendship to instantiated template_clase<view_model>, rather it
is friendship to class template_clase<class view_model>. See what is between
the angle brackets <...>. Your can change view_model to C, T, whatever - it
still will compile and work the same way.
Completely and utterly wrong. Observe:

template <class> class template_clase;

class friend_class {
private:
static void f () { }

#ifdef YOURCODE
friend class template_clase<class view_model>;
#else
template <class> friend class template_clase;
#endif
};

template <class view_model>
class template_clase {
public:
void f () { friend_class::f(); } // 18
};

class view_model { };
template class template_clase<view_model>;
template class template_clase<friend_class>; // 23

If YOURCODE is defined, Comeau gives me the following:

"ComeauTest.c", line 18: error: function "<unnamed>::friend_class::f"
is inaccessible
void f () { friend_class::f(); } // 18
^
detected during instantiation of "void
<unnamed>::template_clase<view_model>::f() [with
view_model=<unnamed>::friend_class]" at line 23

In other words, template_clase<view_model> has access to
friend_class::f(), but not template_clase<friend_class>. Therefore,
your code gives friendship only to template_clase<view_model>.

If YOURCODE is *not* defined, Comeau accepts the code. That means that
only my code gives friendship to both template_clase<view_model> and
template_clase<friend_class> -- in fact, it gives friendship to all
instantiations of template_clase.
2. To give friendship to template class foo :
friend class foo<class TTT>; // CLASS TTT

This is true for MS VC++ compiler ( see original posting).


No, it is not. Visual C++ .NET (aka 7.0) gives friendship as ISO C++,
Comeau, GCC 3.2, and I say it should. It accepts (and rejects) the
above code exactly as all these compilers do.

(NB: Visual C++ 6.0 is broken and cannot give friendship to templates.
It rejects the above code regardless of whether YOURCODE is defined.)

- Shane
Jul 19 '05 #14
WW wrote:

Viatcheslav L. Gorelenkov wrote:
"WW" <wo***@freemail.hu> wrote in message
There is no such thing as template class. There are class
templates. Read this two sentences until you grasp the difference.
Class templates are *only* templates. They are not classes. So
they cannot be made friends.


1. At least at definition we call it "template <typename X> class Y".
:-))


Haha
2. Both "template classes" and "classes" are data abstractions.


There is no such thing as template class.


A class template is a pattern for a class. A template class is an
instantiation of a class template. Read the FAQ!

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Jul 19 '05 #15
WW
David Rubin wrote:
[SNIPO]
There is no such thing as template class.


A class template is a pattern for a class. A template class is an
instantiation of a class template. Read the FAQ!


I read the standard, than you. There is no such thing as template class.

--
WW aka Attila
Jul 19 '05 #16
WW wrote:

David Rubin wrote:
[SNIPO]
There is no such thing as template class.


A class template is a pattern for a class. A template class is an
instantiation of a class template. Read the FAQ!


I read the standard, than you. There is no such thing as template class.


In that case, you should send a correction to the FAQ maintainer:

http://www.parashift.com/c++-faq-lit....html#faq-34.7
"Unlike template functions, template classes (instantiations of class
templates)..."

/david

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Jul 19 '05 #17
WW
David Rubin wrote:
WW wrote:

David Rubin wrote:
[SNIPO]
There is no such thing as template class.

A class template is a pattern for a class. A template class is an
instantiation of a class template. Read the FAQ!
I read the standard, than you. There is no such thing as template
class.


In that case, you should send a correction to the FAQ maintainer:

http://www.parashift.com/c++-faq-lit....html#faq-34.7 "Unlike template functions, template classes (instantiations of class
templates)..."


Again. Read the standard. Or read C++ TEMPLATES, The Complete Guide by
David Vandevoorde & Nicolai M. Josuttis ISBN 0-201-73484-2 on Page 87,
Chapter 7, Basic Template Terminology.""Because of this imprecision, we
avoid the term /template/ /class/ on this book." Before that it list 3,
ancient, all dropped meanings for the "term" (I whish he had used phrase).
Also those who followed the making of the standard also know that term
template class has been dropped all over the standard - because it was an
incorrecmt one. There are (class) template instances. There are template
IDs. There is partial specialization and many many more things. There is
now such things as template class.

--
WW aka Attila
Jul 19 '05 #18
WW wrote:
David Rubin wrote:
WW wrote:
David Rubin wrote:
[SNIPO]

>There is no such thing as template class.

A class template is a pattern for a class. A template class is an
instantiation of a class template. Read the FAQ!

I read the standard, than you. There is no such thing as template
class.


In that case, you should send a correction to the FAQ maintainer:


http://www.parashift.com/c++-faq-lit....html#faq-34.7
"Unlike template functions, template classes (instantiations of class
templates)..."

Again. Read the standard. Or read C++ TEMPLATES, The Complete Guide by
David Vandevoorde & Nicolai M. Josuttis ISBN 0-201-73484-2 on Page 87,
Chapter 7, Basic Template Terminology.""Because of this imprecision, we
avoid the term /template/ /class/ on this book." Before that it list 3,
ancient, all dropped meanings for the "term" (I whish he had used phrase).
Also those who followed the making of the standard also know that term
template class has been dropped all over the standard - because it was an
incorrecmt one. There are (class) template instances. There are template
IDs. There is partial specialization and many many more things. There is
now such things as template class.


This is pretty interesting, and I'll take you at your word that the term
"template class" is deprecated. However, as a clc++ regular (you seem to be), I
thought that you might have some interest in correcting the FAQ since everyone
knows to look at the FAQ, whereas not many people would pick up Vandevoorde and
Josuttis.

/david

--
FORTRAN was the language of choice
for the same reason that three-legged races are popular.
-- Ken Thompson, "Reflections on Trusting Trust"

Jul 19 '05 #19
WW
David Rubin wrote:
[SNIPPODROM]
This is pretty interesting, and I'll take you at your word that the
term "template class" is deprecated.
It isn't. It has never been in the standard.
However, as a clc++ regular (you
seem to be), I thought that you might have some interest in
correcting the FAQ since everyone knows to look at the FAQ, whereas
not many people would pick up Vandevoorde and Josuttis.


Yeah. But it is also known that the one who wrote the FAQ is another man
with a strong opinion. And his definition is one of the three possible. :-)
So IMO I would have a reaaaaly hard time to convince him. But I can give it
a try. :-) But I have to prepare for it. ;-)

--
WW aka Attila
Jul 19 '05 #20
The problem here is that template_class is not a class - it is a
template for creating classes. You can do something like:

friend template_class<foo>;

to make the template_class<foo> class a friend, but
template_class<bar> will not be a friend. To do what you're looking
for would require an extention to the C++ language so that you could
write something like:

template <typename T> friend template_class<T>;

Unfortunatly, this construct simply doesn't exist. I would not be at
all suprised if the designers have a very good reason for this,
although I haven't taken the time to look into it.

"Sebastian Faust" <sf*************@logic-software.de> wrote in message news:<bl*************@news.t-online.com>...
Hi,

is a construction like the following possible:

template<class view_model>
class template_clase
{
protected:
template_clase() {}
virtual ~template_clase() {}
};

clase friend_class
{
friend_class() {}
virtual ~friend_class{}

friend template_class;
};

My compile ( Visual C++ 7.0 ) doesn't accept this code. Is it possible and
if yes how, to use a template class as a friend class?

Thanks in advance
Sebastian

Jul 19 '05 #21
dslater wrote:
The problem here is...


Please don't top-post. Read section 5 of the FAQ for posting guidelines.

http://www.parashift.com/c++-faq-lite/

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Christophe Barbe | last post by:
I am not clear about friend functions of a template class. GCC (3.3.2) wants me to add <> after the friend function names in the class declaration and VisualC++ doesn't like that. template...
1
by: Dmitry D | last post by:
Hi all, I'm having problems with declaring a template friend function. It seems like I've done everything as explained in C++ FAQ, but still, I'm getting the linker error (unresolved external...
5
by: Yoon-Soo Lee | last post by:
I am using Visual C++ .NET 2003 and running into some linking error from the following template code. The error messages is error LNK2019: unresolved external symbol "class...
5
by: Trevor Lango | last post by:
What is the appropriate syntax for placing a friend function that includes as one of it's parameters a pointer to the class object itself within the template class? I have the following: ...
6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
3
by: 胡岳偉(Yueh-Wei Hu) | last post by:
Hi all, I have 2 questions about template function as friends in template classes. I don't know why, and hope someone could help me. ...
0
by: Yueh-Wei Hu | last post by:
Victor Bazarov <v.Abazarov@comAcast.net> wrote in message news: ============================================================== > > Question 1: > >...
8
by: Douglas | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** Hello, The following code does not compile if line 3 is uncommented "using namespace std". I do not understand it. Could...
9
by: Adam Badura | last post by:
I have code like this template<int size> big_int { /* ... */ template<int size2> friend class big_int<size2>; }; What I wanted to achive is to be able to easly convert different sized...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
1
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.