Article View: comp.std.c++
Article #3884Re: double const declarations
From: Fergus Henderson
Date: Mon, 01 Apr 1996 18:42
Date: Mon, 01 Apr 1996 18:42
64 lines
3072 bytes
3072 bytes
jchristo@mit.edu (James Christodouleas) writes:
>fjh@munta.cs.mu.OZ.AU (Fergus Henderson) wrote:
>
>> Philippe Verdy <100105.3120@compuserve.com> writes:
>>
>> >template <class T, class CT>
>> >class SmartPtr2 {
>> > SmartPtr2(T* p) { mp = const_cast<CT>(p) ; }
>> > CT & Dereference() {
>> > return *mp ;
>> > }
>> > private :
>> > CT *mp ;
>> >} ;
>> >template <class T> class ConstSmartPtr : SmartPtr<T, T> {} ;
>> >template <class T> class FreeSmartPtr : SmartPtr<T, const T> {} ;
>>
>> You forgot to delegate the constructors.
[...]
>What does it mean to "delegate constructors?"
Delegation is when the code to implement an method (in this case,
the constructor) in one class just "delegates" the work to someone
else by calling the same method in another class.
Perhaps the simplest way to explain what I meant is to write out the code:
template <class T> class ConstSmartPtr : SmartPtr2<T, T> {
// delegate the constructor
CountSmartPtr(T* p) : SmartPtr2(p) {}
};
template <class T> class FreeSmartPtr : SmartPtr2<T, const T> {} ;
// delegate the constructor
FreeSmartPtr(T* p) : SmartPtr2(p) {}
};
In C++, most operations are automatically delegated from a derived class
to its base class -- this is called inheritence. However, C++ doesn't
provide any special support for delegating constructors. That has to be
done manually.
I also wrote:
>> (Of course, if C++ supported template typedefs, there wouldn't be
>> any need to delegate constructors in examples like this.
>> Unfortunately it doesn't.)
In case that wasn't clear, I meant that instead of the above code, if
C++ supported template typedefs, you could write
template <class T> typedef SmartPtr2<T, T> ConstSmartPtr;
template <class T> typedef SmartPtr2<T, const T> FreeSmartPtr;
--
Fergus Henderson WWW: http://www.cs.mu.oz.au/~fjh
fjh@cs.mu.oz.au PGP: finger fjh@128.250.37.3
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Technical Details
Message-ID:
<4jilcf$abp@mulga.cs.mu.OZ.AU>Path:
arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austernReferences:
<4jc2fa$bqu@arl-news-svc-2.compuserve.com> <4jdvnj$o35@mulga.cs.mu.OZ.AU> <QQajel16319.199603292150@relay1.UU.NET>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austern From: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson) Newsgroups: comp.std.c++ Subject: Re: double const declarations Date: 01 Apr 1996 10:42:38 PST Organization: Comp Sci, University of Melbourne Approved: austern@isolde.mti.sgi.com Message-ID: <4jilcf$abp@mulga.cs.mu.OZ.AU> References: <4jc2fa$bqu@arl-news-svc-2.compuserve.com> <4jdvnj$o35@mulga.cs.mu.OZ.AU> <QQajel16319.199603292150@relay1.UU.NET> NNTP-Posting-Host: isolde.mti.sgi.com X-Original-Date: 30 Mar 1996 06:46:39 GMT X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBMWAjnky4NqrwXLNJAQHWcQIAs1Lw8xw7apgd9uRkPx+Vo6cjRxygJlPd I6vr4Ky7Vs55AdHwIjiZYgerzEONEJF5SKU3wKYskZ30KoPkMIlzeQ== =UmF5 Originator: austern@isolde.mti.sgi.com