Article View: comp.std.c++
Article #3597Some ideas about C++ and a question
From: Valentin Bonnard
Date: Sun, 03 Mar 1996 23:34
Date: Sun, 03 Mar 1996 23:34
162 lines
4681 bytes
4681 bytes
I have 4 ideas about C++ and a question.
1) String literals
~~~~~~~~~~~~~~~~~~
String literals should be const char* instead of char* (if it is not
already the case).
2) Placement operators
~~~~~~~~~~~~~~~~~~~~~~
class Object {
void operator+ // or / or whatever
(Object& result, const Object& b) const;
void operator* (Object& result) const;
Object operator* (const Object& result) const; // old one
}
should be called respectively by:
a = b + c;
and a = *b;
and (old one) a*b;
This will solve the [well-known] problem of copying the result into a temporary.
The compatibility will be preserved since old syntax will be allowed to.
3) Use of explicit keyword
~~~~~~~~~~~~~~~~~~~~~~~~~~
//========= Test.h =========
class Base {
public:
virtual void VirtualFunc ();
};
class Derived : public Base {
public:
void VirtualFunc () { }
};
//========= Caller.cp =========
#include <Test.h>
class Derived2 : public Derived {
public:
void VirtualFunc ();
};
//========= Foo.cp =========
#include <Test.h>
void foo (explicit Derived* object)
{
object.VirtualFunc (); // nop (compiler does not generate code for this)
Derived* AnyDerivedObject = object; // ok
}
void foo2 (Derived* object)
{
object.VirtualFunc (); // object can be Derived2
explicit Derived* RealDerivedObject = object; // error
}
The meaning of explicit could be: while a derived class ptr [resp. ref] can be
convert to a base class ptr [ref], it can't be converted to a base class
explicit ptr [ref].
So explicit would be a type qualifier; this usage is different with:
> Rich Hickey
> rich@rcs-hq.mhs.compuserve.com
> I propose that the explicit keyword be allowed as a qualifier of a class
> definition, such qualification taking the form of:
> explicit class X{
> //the definition of X
> };
Here, X cannot be derived but my Derived can be so I think my idea is better
(please fell free comment/criticise this assertion).
4) Conversion of Type** to const Type**
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
It's ok, this is an error; but why cannot Type** be converted to const Type*
const* ?
void foo ()
{
typedef int Type;
Type i;
const Type ci = 2;
Type* pi = &i;
Type** ppi = π
const Type** ppci = ppi; // error because
*ppci = &ci; // now pi == &ci
*pi = 0; // and now we modify ci
const Type*const* pcpci = ppi; // no problem because
*pcpci = &ci; // this is impossible (since *pcpci is
const)
}
Now a question:
Will there be any way to redefine (overload) normal && or || on class ?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I mean, now:
class Int {
public:
operator int () const { return i; }
operator && (int) const;
operator || (int) const;
Int (int I) : i(I) { }
private:
int i;
};
int Func ()
{
cout << "called\n";
return 1;
}
void foo ()
{
int i (1);
Int I (1);
if (i || Func ()) // called is not printed
;
if (I || Func ()) // called is printed
;
}
One could think that the standard should be change in order to support real
overloaded operator && and ||.
I can't see the point of doing that, so if it's the case for you to don't ask
me nor criticise.
Valentin Bonnard
bonnardv@pratique.fr
[ 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:
<4h9smu$m2n@s3.iway.fr>Path:
arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamageShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage From: Valentin Bonnard <bonnardv@pratique.fr> Newsgroups: comp.std.c++ Subject: Some ideas about C++ and a question Date: 3 Mar 1996 23:34:07 GMT Organization: Internet Way Approved: clamage@eng.sun.com (comp.std.c++) Message-ID: <4h9smu$m2n@s3.iway.fr> NNTP-Posting-Host: taumet.eng.sun.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Nntp-Posting-Host: ts1-p22.dialup.iway.fr X-Mailer: Mozilla 1.1N (Macintosh; I; 68K) To: rich@rcs-hq.mhs.compuserve.com X-Url: news:comp.std.c++#4h4oiv$4md@xanadu.io.com Content-Length: 3505 Originator: clamage@taumet