Article View: comp.std.c++
Article #2848Re: new syntax for new operator
From: Claus Andre Faerber
Date: Mon, 04 Dec 1995 22:33
Date: Mon, 04 Dec 1995 22:33
96 lines
3455 bytes
3455 bytes
pierreba@poster.cae.ca (30 Nov 95):
>[...]
> -- Start example --
>
> class A
> {
> public:
> // default new for static variables.
> static void operator new ( void *, size_t );
> // overloaded new for static variables.
> static void operator new ( void *, size_t, int a, int b );
> // delete for static variables.
> static void operator delete ( void *, size_t );
>
> // default new for auto variables.
> auto void operator new ( void *, size_t );
> // overloaded new for auto variables.
> auto void operat+7 " >/w 2 c+e_t, int a, int b );
> // delete for auto variables.
> auto void operator delete ( void *, size_t );
> };
>
> static A a2; // static A object using declared default new.
> static A a2 = new (88,91) A; // static A object using overloaded new.
>
> int main (void)
> {
> A a3; // auto A object with declared default new.
> A a4 = new (88,91) A; // auto A object with overloaded new.
This sytax is inconsistent with the existing one: new always return a
poiter to the object created.
> ...
>
> } // at this point the declared delete operator is called for each A
>
> -- End example --
Another point is, that you don't need a new operator for static and auto
objects, as they have reserved space in the data segment ("static-heap")
or on the stack, which is allocated at compile time.
Therefore you can't create static or auto objects with sizes calculated at
run time, either.
>[...]
> Usage of the new operators are multiples. The first is the possibility to
> distinguish between stack-allocated, static-heap and dynamic-heap
> variables. Many programs need that capability, notably when related to
> smart pointers and garbage collection. Another usage (the one which
> prompted me to write this) would be in the context of OODBMS. The
> ability to communicate with the server and take different actions depending
> on the location of the variable can dramatically improve the performance
> of some operations.
I suggest defining a new syntax for *constructors* (or any member
functions) instead:
class A
{
A ( ); // Perhaps we should create a new keyword like
A ( int abc ); // "dynamic" for heap objects.
A ( ) static;
A ( int abc ) static;
A ( ) auto;
A ( int abc ) auto;
DoSomething ( int* xyz );
DoSomething ( int* xyz ) static;
DoSomething ( int* xyz ) auto;
~A( );
~A( ) static;
~A( ) auto;
};
This also meets the syntax used by many compiler vendors to differentiate
far and near methodes.
>[...]
===================================================
Claus Andre Faerber <claus@faerber.muc.de>
===================================================
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<5zC9VxOocDB@faerber.muc.de>Path:
arc1-ca.pugleaf.net!news.mindlink.net!agate!agate!MattShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!news.mindlink.net!agate!agate!Matt From: claus@faerber.muc.de (Claus Andre Faerber) Newsgroups: comp.std.c++ Subject: Re: new syntax for new operator Date: 04 Dec 1995 22:33:29 GMT Organization: <none> Approved: Matt@Austern Message-ID: <5zC9VxOocDB@faerber.muc.de> Reply-To: news-admin@faerber.muc.de NNTP-Posting-Host: austern.hip.berkeley.edu