Article View: comp.std.c++
Article #733Re: Suggestion for an alternative pointer/handle/memory consept
From: Pablo Halpern
Date: Tue, 29 Sep 1992 17:50
Date: Tue, 29 Sep 1992 17:50
105 lines
3439 bytes
3439 bytes
In article <1992Sep24.191342.15458@cadsun.corp.mot.com>, shang@corp.mot.com (David (Lujun) Shang) writes:
|> My question is that: is it possible to write class of a special
|> memory reference protocol that can be used exactly the same way
|> as a standard pointer is used in C++?
|>
|> With language-supported pointers, we can write:
|>
|> BaseClass * bp;
|> DerivedClass * dp;
|> ...
|> bp = dp = new DerivedClass(...);
|> bp->SomeVirtualFunction(...);
|> ...
|>
|> "BaseClass*" can be viewed as a type of pointer<BaseClass>, and
|> "DerivedClass*" as pointer<DerivedClass>, where pointer is a
|> standard memory reference.
|>
|> Now, we consider the MS Windows's memory protocol. Is it possible
|> to write codes as follows:
|>
|> HANDLE<BaseClass> bh;
|> HANDLE<DerivedClass> dh;
|> ...
|> bh = dh = new DerivedClass(...);
|> bh->SomeVirtualFunctions(...);
|>
|> Further, is it possible to write codes that independent to any
|> particular memory reference protocols? Like:
|>
|> REF<BaseClass> bref;
|> REF<DerivedClass> dref;
|> ...
|> bref = dref = new DerivedClass(...);
|> bref->SomeVirtualFunctions(...);
I do not have a complete solution, but here are some ideas:
Implement a base template class with a pointer-like interface:
template <class T>
class INDIRECT
{
public:
INDIRECT();
INDIRECT(const INDIRECT &);
virtual ~INDIRECT();
virtual T& operator *();
virtual T* operator ->() { return & this->operator*(); }
virtual INDIRECT& New(params);
virtual void Delete();
// Assignment and equality operators, etc. ...
};
Then derive a class for each special kind of indirection.
template <class T>
class SMART_POINTER : public INDIRECT<T>
{
...
};
template <class T>
class HANDLE : public INDIRECT<T>
{
...
};
Instead of using operator new(), you call the New() member function on the
indirect object:
i.New(args); instead of i = new T(args);
One of the biggest disadvantage to this approach is that the New()
function must take the same parameters regardless of the type of T. This
can be as follows:
template <class T>
class INDIRECT
{
...
virtual INDIRECT& New(const T&);
...
};
HANDLE<String> hs;
hs.New(String("abc"));
This is, of course, very kludgy and could result in undesirable side
effects if the constructor for String is called twice. I told you it
wasn't a complete solution. Just some food for thought.
- Pablo
------------------------------------------------------------------------
Pablo Halpern (617) 290-3542
HP Waltham pabloh@hpwarq.wal.hp.com
I am self-employed, so my opinions *do* reflect those of my employer.
However, they may not reflect the opinions of my client.
------------------------------------------------------------------------
Technical Details
Message-ID:
<3742@hpwala.wal.hp.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!sun-barr!olivea!spool.mu.edu!sdd.hp.com!scd.hp.com!hplextra!hpcc05!hpyhde4!hpycla!hpergfg2!hpda!hpwala!pablohReferences:
<1992Sep18.012044.28877@ucc.su.OZ.AU> <1992Sep24.191342.15458@cadsun.corp.mot.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!sun-barr!olivea!spool.mu.edu!sdd.hp.com!scd.hp.com!hplextra!hpcc05!hpyhde4!hpycla!hpergfg2!hpda!hpwala!pabloh From: pabloh@hpwala.wal.hp.com (Pablo Halpern ) Newsgroups: comp.std.c++ Subject: Re: Suggestion for an alternative pointer/handle/memory consept Message-ID: <3742@hpwala.wal.hp.com> Date: 29 Sep 92 17:50:41 GMT References: <1992Sep18.012044.28877@ucc.su.OZ.AU> <1992Sep24.191342.15458@cadsun.corp.mot.com> Sender: netnews@hpwala.wal.hp.com Organization: Hewlett-Packard Company Lines: 105