Hiding class pointers -- was it a good idea?
Daniel919
Daniel919 at web.de
Thu Aug 16 16:51:13 PDT 2007
Matthias Walter schrieb:
> Bill Baxter Wrote:
>
>> eao197 wrote:
>>> On Thu, 16 Aug 2007 01:35:17 +0400, Walter Bright
>>> <newshound1 at digitalmars.com> wrote:
>>>
>>>> 3) Value types just don't work for polymorphic behavior. They must be
>>>> by reference. There's no way in C++ to ensure that your class
>>>> instances are used properly by reference only (hence (2)). In fact, in
>>>> C++, it's *extra work* to use them properly.
>>> But from another side all types in C++ (internal or user defined) is a
>>> first class citizens. It is especially important in generic programming,
>>> where I can write:
>>>
>>> template< class T >
>>> class ValueHolder {
>>> T * m_value
>>> public :
>>> ValueHolder() : m_value( new T() ) {}
>>> ...
>>> };
>>>
>>> and this code will work as with int, as with std::string, as with
>>> ValueHolder<SomeAnotherType>.
>> I hear you. Fortunately it's pretty trivial to throw some
>> static-if-is-zzle at the problem in D. May not be so pretty, but it's
>> straightforward at least.
class ValueHolder(T) {
private:
T m_value;
public:
this() {
static if(is(T == class))
m_value = new T;
else
m_value = *(new T);
}
}
> What about letting "new int" do nothing and new int (2) be a simple "2"?
> Then, T foo = new T(); and T foo = new T(2); would work, too.
I like this idea. Let me give an example:
struct stPoint { int x,y; }
class clPoint { int x,y; }
stPoint p = new stPoint;
clPoint p = new clPoint;
The former is not working, atm it has to be:
stPoint p = *(new stPoint);
new Something
Intuitively I would expect it to return an instance of type Something.
If I wanted a ptr, I would write:
&(new Something)
and get a: Something*
This looks consistent to me.
Any situation where it could cause trouble ?
More information about the Digitalmars-d
mailing list