Topic: static functions defined inside of classes
Author: ttyp32000@yahoo.com (jonathan)
Date: Wed, 12 Mar 2003 20:50:58 +0000 (UTC) Raw View
brok@rubikon.pl ("Bronek Kozicki") wrote in message news:<a58c4$3e6741eb$c299d863$17936@nf1.news-service.com>...
> "Vladimir Shiryaev" <vladimir.shiryaevNOSPAM@NOSPAMsympatico.ca> wrote:
> >> char& operator[](...) // lvalue
> >
> > It involves a dangling reference problem (or you may have
> > to introduce a proxy class).
>
> only if you return reference to local variable, which is obvious design
> error. You may safely return reference to object stored outside of
> function scope (like member variable or some tiny part of it).
>
>
> > I think what Jonathan means is why there is no way to implement
> > subscripting by value, like:
>
> what's wrong with "operator[]" sematics, like:
>
> char& operator[](size_t index) ;
> char const& operator[](size_t index) const;
>
>
> ?
>
> I personally think that the other problem mentioned by Jon is more
> interesting, namely initialization of static member variables of class.
> Unfortunatelly, I have nothing more to say in but "backward
> compatibility with existing code".
>
But I don't see this as an issue... as long as it can be translated
back to existing C++:
ie:
class AA
{
static myobj myval(xx) is init;
public:
AA() { }
void print() { myval.print(); }
};
==
class myclass
{
static int _count;
static myobj *myval;
friend class myclass_destroy;
AA() { if (!_count++) myval = new myobj(xx); }
void print() { myval->print(); }
};
static class myclass_destroy
{
static int _count;
public:
myclass_destroy( _count++ )
~myclass_destroy()
{
if ( --counter == 0 && myclass::myval )
delete myclass::myval;
}
} destroy_it;
(plus in implementation file):
myclass::myobj *myval;
(although this isn't an exact match -- *myobj vs myobj.) Figure:
1) eight lines of source code versus 30
2) no 'friend' declarations
3) both guarantee statics being initialized at point of usage
4) 2 files of definition versus one, possibility for
self-contained header files dropped into source code (ala boost)
5) allocation on stack rather than on heap, no messy clean up
issues.
6) co-exists with current source code (static... is init)
currently an error
7) consistent with function declaration of statics.
The only drawback I see is with multi-threading issues on creation,
although I think that this could be gotten around pretty easy by
saying that the 'is init' flag guarantees that the statics are created
*by the end of the first constructor* rather than the beginning,
giving the user the ability to mutex the constructor properly.
jon
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: vladimir.shiryaevNOSPAM@NOSPAMsympatico.ca ("Vladimir Shiryaev")
Date: Thu, 6 Mar 2003 00:32:04 +0000 (UTC) Raw View
> I'm not sure I understand your request very well, but this is
> already possible (and not only for operator[]):
>
> char& operator[](...) // lvalue
It involves a dangling reference problem (or you may have
to introduce a proxy class).
I think what Jonathan means is why there is no way to implement
subscripting by value, like:
char "operator-subscript-get" (int index);
void "operator-subscript-set" (int index, char value);
Borland Delphi has something like this:
name:[index: integer]: char read GetValue write SetValue; default;
function GetValue(index: integer);
procedure SetValue(index: integer; value: char);
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: brok@rubikon.pl ("Bronek Kozicki")
Date: Thu, 6 Mar 2003 16:36:04 +0000 (UTC) Raw View
"Vladimir Shiryaev" <vladimir.shiryaevNOSPAM@NOSPAMsympatico.ca> wrote:
>> char& operator[](...) // lvalue
>
> It involves a dangling reference problem (or you may have
> to introduce a proxy class).
only if you return reference to local variable, which is obvious design
error. You may safely return reference to object stored outside of
function scope (like member variable or some tiny part of it).
> I think what Jonathan means is why there is no way to implement
> subscripting by value, like:
what's wrong with "operator[]" sematics, like:
char& operator[](size_t index) ;
char const& operator[](size_t index) const;
?
I personally think that the other problem mentioned by Jon is more
interesting, namely initialization of static member variables of class.
Unfortunatelly, I have nothing more to say in but "backward
compatibility with existing code".
Regards
B.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: rogero@howzatt.demon.co.uk ("Roger Orr")
Date: Thu, 6 Mar 2003 22:25:16 +0000 (UTC) Raw View
"jonathan" <ttyp32000@yahoo.com> wrote in message
news:f05e664d.0303031625.32ac395f@posting.google.com...
> hey all,
[snip]
> And finally, how does one create a formal proposal to get these ideas
> implemented? What's required?
Formal proposals are usually made through your national body - its in the
FAQ...
http://www.jamesd.demon.co.uk/csc/faq.html#B11
But people normally bounce ideas around for a while informally - often in
this newsgroup - to get the rough edges smoothed down.
Roger Orr
--
MVP in C++ at www.brainbench.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: ttyp32000@yahoo.com (jonathan)
Date: Tue, 4 Mar 2003 00:56:46 +0000 (UTC) Raw View
hey all,
I was wondering exactly why there wasn't a flag/modifier/whatever so
that you
could say:
class myclass
{
static obj myobj(44) is init;
};
and have the object initialized *the first time a myclass object is
created*. It's frustrating as all hell to have to wrap all of the
static members inside
of functions to get this behaviour - its error prone, ugly, etc. And
on the subject, why can't there be a modifier to operator[], ie:
class myclass
{
char operator[](...) is lvalue
{
}
};
or rvalue? Having to go through the machinations to make a[val] = 1;
and cerr << a[val] << endl; execute different pieces of code is *also*
inelegent, error prone and ugly.
And finally, how does one create a formal proposal to get these ideas
implemented? What's required?
jon
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: falk.tannhauser@crf.canon.fr (=?ISO-8859-1?Q?Falk_Tannh=E4user?=)
Date: Tue, 4 Mar 2003 13:29:44 +0000 (UTC) Raw View
jonathan wrote:
> And on the subject, why can't there be a modifier to operator[], ie:
>
> class myclass
> {
> char operator[](...) is lvalue
> {
> }
> };
>
> or rvalue? Having to go through the machinations to make a[val] = 1;
> and cerr << a[val] << endl; execute different pieces of code is *also*
> inelegent, error prone and ugly.
I'm not sure I understand your request very well, but this is
already possible (and not only for operator[]):
char operator[](...) // rvalue
char& operator[](...) // lvalue
char const& operator[](...) // constant lvalue
Falk
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: vladimir.shiryaevNOSPAM@NOSPAMsympatico.ca ("Vladimir Shiryaev")
Date: Wed, 5 Mar 2003 03:54:46 +0000 (UTC) Raw View
I'm not sure I understand your request very well, but this is
> already possible (and not only for operator[]):
>
> char& operator[](...) // lvalue
It involves a dangling reference problem (or you may have
to introduce a proxy class).
I think what Jonathan means is why there is no way to implement
subscripting by value, like:
char "operator-subscript-get" (int index);
void "operator-subscript-set" (int index, char value);
Borland Delphi has something like this:
name:[index: integer]: char read GetValue write SetValue; default;
function GetValue(index: integer);
procedure SetValue(index: integer; value: char);
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]
Author: vladimir.shiryaevNOSPAM@NOSPAMsympatico.ca ("Vladimir Shiryaev")
Date: Wed, 5 Mar 2003 07:09:44 +0000 (UTC) Raw View
> I'm not sure I understand your request very well, but this is
> already possible (and not only for operator[]):
>
> char& operator[](...) // lvalue
It involves a dangling reference problem (or you may have
to introduce a proxy class).
I think what Jonathan means is why there is no way to implement
subscripting by value, like:
char "operator-subscript-get" (int index);
void "operator-subscript-set" (int index, char value);
Borland Delphi has something like this:
name:[index: integer]: char read GetValue write SetValue; default;
function GetValue(index: integer);
procedure SetValue(index: integer; value: char);
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]