From 421958432648860619
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,75a9ad8b6ce88ef7
X-Google-Attributes: gidf78e5,public
From: Valentin Bonnard <bonnardv@pratique.fr>
Subject: Re: new class initializer syntax?
Date: 1997/01/31
Message-ID: <32F26616.1C9C@pratique.fr>#1/1
X-Deja-AN: 213553317
references: <upvypkzc9.fsf@worldnet.att.net> <32F0D31A.4C65@empathy.com>
x-original-date: Fri, 31 Jan 1997 21:37:26 +0000
organization: Internet Way
x-auth: PGPMoose V1.1 PGP comp.std.c++
newsgroups: comp.std.c++
originator: austern@isolde.mti.sgi.com
x-mailer: Mozilla 2.0 (Macintosh; I; 68K)


John Lilley wrote:
> 
> Without changing the language, you can still accomplish what you want
> through some class wrappers.  I've done this for "ORed flag" classes to
> (a) always initialize them (b) disallow copying to incompatible
> classes.  You can implement e.g. a SmartInt:
> 
> class SmartInt {
> public:
>    SmartInt(int v_ = 0) : v(v_) {}
>    operator int() const { return v; }
>    // other conversion operators (mostly unneeded).
> 
>    SmartInt& operator=(const SmartInt&rhs) { v=rhs.v; return this; }
>    SmartInt& operator=(const int&rhs) { v=rhs; return this; }
>    // other assignment operators...
> private:
>    int v;
> };
> 
> This is not perfect -- you lose some implicit conversion that you get
> with real 'int' 

You wrote the operator int and the ctor !

So the std conversions are still legal, but htey rae now user defined
conversions so other user defined are not possible to have annother
user defined:

void foo (complex<float>);

SmartInt i = 4;
foo (i);

This won't work, but work fine with plain int.

In others contexts, it will work fine.

> and you cannot specify that conversion to 'int' takes
> priority over conversion to other types. 

It does

> Performance is usually no
> problem, since all methods are inline and trivial. 

It is: the compiler assume that a struct is never trivial,
and method are sometimes not inlined.

> You also need to
> write various expression operators. 

Not all of them:

SmartInt b, c;
SmartInt a = b + c;

will work;

b += c;

won't

> In general, it's a big pain in the
> butt.  But I've found that in many cases it is better to have the
> guarantee of initialization and deal with the other headaches.

I think it's better to innitialise explicitly.

Does anyone knows a compiler/tool to catch uninitialised vars ?

-- 

Valentin Bonnard
mailto:bonnardv@pratique.fr
http://www.pratique.fr/~bonnardv (Informations sur le C++ en Francais)
---
[ 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 
]



