Article View: comp.sys.mac.programmer.codewarrior
Article #1091Re: Problem with friendly templates
From: meeroh
Date: Tue, 20 Nov 2001 05:13
Date: Tue, 20 Nov 2001 05:13
66 lines
3216 bytes
3216 bytes
In article <191120012332200672%potswa@mac.nospam.com>,
David 'Potatoswatter' Krauss <potswa@mac.nospam.com> wrote:
>[[ This message was both posted and mailed: see
> the "To," "Cc," and "Newsgroups" headers for details. ]]
>
>In article
><mjolnir_DELETE_-279068.14135819112001@east.usenetserver.com>, Adin
>Hunter Baber <mjolnir_DELETE_@soltec.net> wrote:
>
>> I was told that if you want to have a template type be friends with one
>> another you need to declare it like this:
>>
>> template<class T>
>> class temClass
>> {
>> public:
>> ...
>> template<class U> friend class temClass<U>;
>> ...
>> };
>>
>> However, if i write my code this way I get a "declaration syntax error"
>> and the Error & Warnings Window has friend underlined.
>
>Nah - I don't see how this could work. The basic principle behind
>templates is that they have to be instantiated before anything is
>actually declared. "friend" statements are more directives than
>declarations - you can't instantiate them.
It can work rather simply.
template <class T>
class c1 {
// blah blah
};
template <class T>
class c2 {
template <class U> friend class c1 <U>;
};
except that CWP7 doesn't support friend templates yet, which is why this
code won't compile.
>> One work around, that appears to work fine, is have the templates all
>> derive from a common base class. The base class contains the data that
>> needs to be shared. When the sharing of data is necessary, one of the
>> classes is upcast, and the appropriate data is accessed.
>
>This is always a good idea. Putting as much functionality as possible
>into base classes optimizes your programs, makes debugging easier, and
>makes the gods love you more ;v) . Ideally, templates should only be
>"wrappers" that declare no non-inline functions.
I think you are grossly over-generalizing. There are many cases when
it's very wrong to put as much functionality as possible into base
classes, and the answer to such questions is highly dependent on the
problem domain.
(and since we are given no information about the problem domain, I don't
have much useful to say...)
meeroh
--
Hire me: <http://meeroh.org/hire.html>
Technical Details
Message-ID:
<macdev-3986F0.00130720112001@senator-bedfellow.mit.edu>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!bloom-beacon.mit.edu!senator-bedfellow.mit.edu!dreaderd!not-for-mailReferences:
<mjolnir_DELETE_-279068.14135819112001@east.usenetserver.com> <191120012332200672%potswa@mac.nospam.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10bcce,45060c4a2ff696d4 X-Google-Attributes: gid10bcce,public X-Google-Thread: f635e,45060c4a2ff696d4 X-Google-Attributes: gidf635e,public X-Google-ArrivalTime: 2001-11-19 21:13:08 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!bloom-beacon.mit.edu!senator-bedfellow.mit.edu!dreaderd!not-for-mail From: meeroh <macdev@meeroh.org> Newsgroups: comp.sys.mac.programmer.codewarrior,codewarrior.mac Mail-Copies-To: poster Subject: Re: Problem with friendly templates Organization: MIT References: <mjolnir_DELETE_-279068.14135819112001@east.usenetserver.com> <191120012332200672%potswa@mac.nospam.com> User-Agent: MT-NewsWatcher/3.1 (PPC) Date: Tue, 20 Nov 2001 00:13:07 -0500 Message-ID: <macdev-3986F0.00130720112001@senator-bedfellow.mit.edu> Lines: 66 NNTP-Posting-Host: TITANIUM-48.MIT.EDU X-Trace: 1006233187 senator-bedfellow.mit.edu 3926 18.101.0.199