Topic: request for comments - static class
Author: "chris" <chris@bubblescope.net>
Date: Mon, 15 Apr 2002 12:17:33 GMT Raw View
Hello
I've recently been doing some programming in which I have a class I know
there will only be one copy of in my program. As it is used in my program's
inner-loop, I added the word "static" before all the members and functions,
and the code is now slightly faster, as there is no need to pass around a
function pointer. This also stops people creating multiple copies of a class
which it would make no sense to have more than one copy of.
However, I would like to be able to write the following code:
static class bla {
int x;
public:
bla(void) {x=1;}
int returnx(void) {return x;}
};
int main(void) {
printf("%d",bla.returnx());
return 0;
}
//end of code
Which I feel looks much neater and less error-prone. What is the best way to
get this kind of effect in current C++, and what reasons are there not to
add this to C++?
Thank you for your time.
---
[ 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: rhairgroveNoSpam@Pleasebigfoot.com (Bob Hairgrove)
Date: Mon, 15 Apr 2002 15:37:53 GMT Raw View
On Mon, 15 Apr 2002 12:17:33 GMT, "chris" <chris@bubblescope.net>
wrote:
>Hello
>
>I've recently been doing some programming in which I have a class I know
>there will only be one copy of in my program. As it is used in my program's
>inner-loop, I added the word "static" before all the members and functions,
>and the code is now slightly faster, as there is no need to pass around a
>function pointer. This also stops people creating multiple copies of a class
>which it would make no sense to have more than one copy of.
This is commonly known as the "Singleton" pattern. There are various
ways of implementing a singleton; I'm not sure that I have ever seen
one like this, though.
>
>However, I would like to be able to write the following code:
>
>static class bla {
>int x;
>public:
>bla(void) {x=1;}
>int returnx(void) {return x;}
>};
>
>int main(void) {
>printf("%d",bla.returnx());
>return 0;
>}
>//end of code
>
>Which I feel looks much neater and less error-prone. What is the best way to
>get this kind of effect in current C++, and what reasons are there not to
>add this to C++?
>
Well, the existing way to call static member functions looks very
similar:
int main(void) {
printf("%d",bla::returnx());
return 0;
}
The "." syntax is reserved for calling non-static member functions of
an instantiated object of a class. This would certainly confuse a lot
of people who are used to doing it the standard way.
A singleton object is nevertheless an object. A purely static object
would be no different from having non-member static functions in a
namespace; you could not take the reference of such an object or use a
pointer to it. That's a big difference.
Why do you feel the dot syntax would be less error-prone ... compared
to what?
Bob Hairgrove
rhairgroveNoSpam@Pleasebigfoot.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: "chris" <chris@bubblescope.net>
Date: Mon, 15 Apr 2002 19:25:05 GMT Raw View
(comment at bottom)
"Bob Hairgrove" <rhairgroveNoSpam@Pleasebigfoot.com> wrote in message
news:3cbacff7.20918348@news.ch.kpnqwest.net...
> On Mon, 15 Apr 2002 12:17:33 GMT, "chris" <chris@bubblescope.net>
> wrote:
>
> >Hello
> >
> >I've recently been doing some programming in which I have a class I know
> >there will only be one copy of in my program. As it is used in my
program's
> >inner-loop, I added the word "static" before all the members and
functions,
> >and the code is now slightly faster, as there is no need to pass around a
> >function pointer. This also stops people creating multiple copies of a
class
> >which it would make no sense to have more than one copy of.
>
> This is commonly known as the "Singleton" pattern. There are various
> ways of implementing a singleton; I'm not sure that I have ever seen
> one like this, though.
>
> >
> >However, I would like to be able to write the following code:
> >
> >static class bla {
> >int x;
> >public:
> >bla(void) {x=1;}
> >int returnx(void) {return x;}
> >};
> >
> >int main(void) {
> >printf("%d",bla.returnx());
> >return 0;
> >}
> >//end of code
> >
> >Which I feel looks much neater and less error-prone. What is the best way
to
> >get this kind of effect in current C++, and what reasons are there not to
> >add this to C++?
> >
>
> Well, the existing way to call static member functions looks very
> similar:
>
> int main(void) {
> printf("%d",bla::returnx());
> return 0;
> }
>
> The "." syntax is reserved for calling non-static member functions of
> an instantiated object of a class. This would certainly confuse a lot
> of people who are used to doing it the standard way.
>
> A singleton object is nevertheless an object. A purely static object
> would be no different from having non-member static functions in a
> namespace; you could not take the reference of such an object or use a
> pointer to it. That's a big difference.
>
> Why do you feel the dot syntax would be less error-prone ... compared
> to what?
>
the reason it would be less error-prone to define this structure at all,
would be it would make it clear from the class definition that the class was
static, else people may try introducing non-static members to the class.
The reason I would like the code to look like this is at previously, I have
had piece of code that look like (and have seen piece of code that look
like)
class foo_classdef { ... }
foo_classdef foo;
...
foo.x=1;
which could be replaced with:
static class foo { ... }
....
foo.x=1;
and allow code compatability.
Personally, I was not aware that "." was used primeraly for non-static
functions. perhaps in this case the use of "::" would be forced. However, it
would still be nice to be able to write "static class" to make all members
static. this would allow someone from just looking at the class-def line to
see the class was static, rather than having to read the whole definition to
see if it was totally static, or just mostly
---
[ 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: "Ludwig Pumberger" <pumberger@direkt.at>
Date: Mon, 15 Apr 2002 21:35:10 GMT Raw View
> However, I would like to be able to write the following code:
>
> static class bla {
> int x;
> public:
> bla(void) {x=1;}
> int returnx(void) {return x;}
> };
>
> int main(void) {
> printf("%d",bla.returnx());
> return 0;
> }
> //end of code
>
> Which I feel looks much neater and less error-prone. What is the best way
to
> get this kind of effect in current C++, and what reasons are there not to
> add this to C++?
>
> Thank you for your time.
>
static class X
{
//...
} x;
means the same as:
class X
{
//...
};
static X x;
This could be confusing.
---
[ 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: "Wesley J. Landaker" <wjl@ee.byu.edu>
Date: Mon, 15 Apr 2002 21:41:44 GMT Raw View
On Monday 15 April 2002 01:25 pm, in comp.std.c++, chris wrote:
> which could be replaced with:
> static class foo { ... }
> ....
> foo.x=1;
>
> and allow code compatability.
>
> Personally, I was not aware that "." was used primeraly for non-static
> functions. perhaps in this case the use of "::" would be forced. However,
> it would still be nice to be able to write "static class" to make all
> members static. this would allow someone from just looking at the
> class-def line to see the class was static, rather than having to read the
> whole definition to see if it was totally static, or just mostly
So you want a "static class" that has members that are all static and can't
be instantiated accidentally...
How about a namespace? Adapting your original example:
namespace bla {
namespace _private { int x = 1; }
int returnx() {return x;}
};
int main() {
printf("%d",bla::returnx());
return 0;
}
--
Wesley J. Landaker - wjl@icecavern.net
OpenPGP FP: C99E DF40 54F6 B625 FD48 B509 A3DE 8D79 541F F830
---
[ 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: Michel Roelofs <michel.roelofs@philips.com>
Date: Tue, 16 Apr 2002 16:26:29 GMT Raw View
chris wrote:
> Hello
>
> I've recently been doing some programming in which I have a class I know
> there will only be one copy of in my program. As it is used in my program's
> inner-loop, I added the word "static" before all the members and functions,
> and the code is now slightly faster, as there is no need to pass around a
> function pointer. This also stops people creating multiple copies of a class
> which it would make no sense to have more than one copy of.
>
> However, I would like to be able to write the following code:
>
> static class bla {
> int x;
> public:
> bla(void) {x=1;}
> int returnx(void) {return x;}
> };
>
> int main(void) {
> printf("%d",bla.returnx());
> return 0;
> }
> //end of code
>
What about an unnamed class:
class {
int x;
public:
void bla() { x=1; }
int returnx() { return x; }
} oneObject;
int main(int, char**) {
return 0;
}
But probably you are best off with a real singleton implementation.
Michel
---
[ 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 ]