Article View: comp.std.c++
Article #3836Re: Suggestion to the C++ standard
From: Jason D. Morris
Date: Wed, 27 Mar 1996 21:44
Date: Wed, 27 Mar 1996 21:44
61 lines
2181 bytes
2181 bytes
>C++ is an OO language, and of course supports encapsulation.
>Problem is - there is no standard way to add access methods.
>For example:
>class foo
>{
>public:
> SetMember( Type aMember );
> Type Member( );
>private:
> Type Member;
>};
#ifndef PROPERTY_H__
#define PROPERTY_H__
// The property class controls access to a captive variable.
// It captures the get/set pattern. This class first appeared
// in the November/December 1995 Vol. 7 / No. 9 Issue of C++ Report
// in the article Patterns in Practice: A property template for C++
// by Colin Hastie.
template < class T >
class property
{
private:
T content; // captive variable
// non-implemented non-accessible assignment operator
void operator =( const property< T >& );
public:
// ctors
property() {}
property< T >( const T& t )
: content( t )
{}
// accessors
T operator()() const // get
{ return content; }
void operator()( const T& t ) // set
{ content = t; }
};
#endif
To use this class you'd do something like this...
class foo
{
public:
property< int > foo_number;
};
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Technical Details
Message-ID:
<31599c95.4890732@nntp.ix.netcom.com>Path:
arc1-ca.pugleaf.net!in1.uu.net!bounce-backReferences:
<4jatnm$s9b@post.tau.ac.il>Show Full Headers
Complete Article Headers
From: jdmorris@ix.netcom.com (Jason D. Morris) Message-ID: <31599c95.4890732@nntp.ix.netcom.com> X-Original-Date: Wed, 27 Mar 1996 19:54:40 GMT Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back Date: 27 Mar 96 21:44:38 GMT Approved: fjh@cs.mu.oz.au Newsgroups: comp.std.c++ Subject: Re: Suggestion to the C++ standard Organization: Netcom References: <4jatnm$s9b@post.tau.ac.il> X-Netcom-Date: Wed Mar 27 11:53:46 AM PST 1996 X-Newsreader: Forte Agent .99d/32.182 X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMVm22OEDnX0m9pzZAQFqSQF/VxDH9TfUGMpUtczlgWYYgtqNL0GmlChy IYteF4Q4SVBUuWg/fX9YjrDV6Zx027mR =5Leu