 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Joe Guest
|
Posted: Tue Mar 02, 2004 9:27 pm Post subject: Defining constants in classes |
|
|
This is a very basic question, but why can't I do the following ?
class schedule {
private:
static const string mScheduleFile = "schedule.txt";
....
}
I hope it is clear what I am trying to do. In the member functions, I then
want to "fout.open(mScheduleFile)".
Is there way of doing this that actually works?
TIA,
Joe.
|
|
| Back to top |
|
 |
Jonathan Turkanis Guest
|
Posted: Tue Mar 02, 2004 9:40 pm Post subject: Re: Defining constants in classes |
|
|
"Joe" <anon (AT) anon (DOT) com> wrote
| Quote: | This is a very basic question, but why can't I do the following ?
class schedule {
private:
static const string mScheduleFile = "schedule.txt";
...
}
|
The language rules only permit in-class initialization of static const
members of integral type.
You can get the effect you're after with a static member function
returning a string or string literal, like so:
const std::string& schedule_file()
{
static const std::string file("schedule.txt");
return file;
}
or
const char* schedule_file()
{
static const char* file = "schedule.txt";
return file;
}
Jonathan
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Tue Mar 02, 2004 9:53 pm Post subject: Re: Defining constants in classes |
|
|
* "Jonathan Turkanis" <technews (AT) kangaroologic (DOT) com> schriebt:
| Quote: |
"Joe" <anon (AT) anon (DOT) com> wrote in message
news:9Y61c.2391$XB.1720 (AT) newsfe1-gui (DOT) server.ntli.net...
This is a very basic question, but why can't I do the following ?
class schedule {
private:
static const string mScheduleFile = "schedule.txt";
...
}
The language rules only permit in-class initialization of static const
members of integral type.
|
Yes, but the OP asked why.
The answer to that seems to be "nobody knows why".
The language just turned out that way through historic accidents etc.
| Quote: | You can get the effect you're after with a static member function
returning a string or string literal, like so:
const std::string& schedule_file()
{
static const std::string file("schedule.txt");
return file;
}
or
const char* schedule_file()
{
static const char* file = "schedule.txt";
return file;
}
|
Or the constant can be defined in the class implementation file:
============ Schedule.hpp =============
#ifndef SCHEDULE_H
#include <string>
class Schedule
{
private:
static std::string const mScheduleFile;
};
#endif
============ Schedule.cpp =============
#include <Schedule.hpp>
std::string const Schedule::mScheduleFile = "schedule.txt";
|
|
| Back to top |
|
 |
Joe Guest
|
Posted: Tue Mar 02, 2004 9:56 pm Post subject: Re: Defining constants in classes |
|
|
Thanks for the quick reply. That works fine, but I can't help feeling it
makes things a a little "untidy".
Is that a standard solution to a standard problem?
Or am I going about the whole problem in an unusual/bad way?
Joe.
"Jonathan Turkanis" <technews (AT) kangaroologic (DOT) com> wrote
| Quote: |
"Joe" <anon (AT) anon (DOT) com> wrote in message
news:9Y61c.2391$XB.1720 (AT) newsfe1-gui (DOT) server.ntli.net...
This is a very basic question, but why can't I do the following ?
class schedule {
private:
static const string mScheduleFile = "schedule.txt";
...
}
The language rules only permit in-class initialization of static const
members of integral type.
You can get the effect you're after with a static member function
returning a string or string literal, like so:
const std::string& schedule_file()
{
static const std::string file("schedule.txt");
return file;
}
or
const char* schedule_file()
{
static const char* file = "schedule.txt";
return file;
}
Jonathan
|
|
|
| Back to top |
|
 |
Joe Guest
|
Posted: Tue Mar 02, 2004 10:02 pm Post subject: Re: Defining constants in classes |
|
|
"Joe" <anon (AT) anon (DOT) com> wrote
| Quote: | Thanks for the quick reply. That works fine, but I can't help feeling it
makes things a a little "untidy".
Is that a standard solution to a standard problem?
Or am I going about the whole problem in an unusual/bad way?
Joe.
"Jonathan Turkanis" <technews (AT) kangaroologic (DOT) com> wrote in message
news:c22uv5$1oqo3p$1 (AT) ID-216073 (DOT) news.uni-berlin.de...
"Joe" <anon (AT) anon (DOT) com> wrote in message
news:9Y61c.2391$XB.1720 (AT) newsfe1-gui (DOT) server.ntli.net...
This is a very basic question, but why can't I do the following ?
class schedule {
private:
static const string mScheduleFile = "schedule.txt";
...
}
The language rules only permit in-class initialization of static const
members of integral type.
You can get the effect you're after with a static member function
returning a string or string literal, like so:
const std::string& schedule_file()
{
static const std::string file("schedule.txt");
return file;
}
or
const char* schedule_file()
{
static const char* file = "schedule.txt";
return file;
}
Jonathan
|
Sorry for the top-post!!
(I have never understood why that is a big deal - it makes life so much
faster if you don't have to scroll, especially for a short post like this.
If anyone has a good reason for why people don't like it, can they tell me?
I will sleep better if I know)
Joe.
|
|
| Back to top |
|
 |
Jonathan Turkanis Guest
|
Posted: Tue Mar 02, 2004 10:13 pm Post subject: Re: Defining constants in classes |
|
|
"Joe" <anon (AT) anon (DOT) com> wrote
| Quote: | Thanks for the quick reply. That works fine, but I can't help
feeling it
makes things a a little "untidy".
Is that a standard solution to a standard problem?
Or am I going about the whole problem in an unusual/bad way?
|
It's one standard solution. Alf gave another one. I think they're both
a bit untidy.
I tend to use the one I showed you because I write a lot of
header-only libraries.
(I' not against top-posting, as such. I think it's like always driving
on the right or left side of the road; it doesn't matter as long as
everyone does the same thing. )
Jonathan
|
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Tue Mar 02, 2004 11:27 pm Post subject: Re: Defining constants in classes |
|
|
"Alf P. Steinbach" wrote:
| Quote: |
* "Jonathan Turkanis" <technews (AT) kangaroologic (DOT) com> schriebt:
"Joe" <anon (AT) anon (DOT) com> wrote in message
news:9Y61c.2391$XB.1720 (AT) newsfe1-gui (DOT) server.ntli.net...
This is a very basic question, but why can't I do the following ?
class schedule {
private:
static const string mScheduleFile = "schedule.txt";
...
}
The language rules only permit in-class initialization of static const
members of integral type.
Yes, but the OP asked why.
The answer to that seems to be "nobody knows why".
The language just turned out that way through historic accidents etc.
|
The reason is that in-class initialization of static const members was
added in order to support their use as compile-time constants. Arrays of
char, floats, etc. can't be used as compile-time constants, so can't be
initialized in this way.
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
|
|
| Back to top |
|
 |
Julie Guest
|
Posted: Tue Mar 02, 2004 11:47 pm Post subject: Re: Defining constants in classes |
|
|
Joe wrote:
| Quote: | Sorry for the top-post!!
(I have never understood why that is a big deal - it makes life so much
faster if you don't have to scroll, especially for a short post like this.
If anyone has a good reason for why people don't like it, can they tell me?
I will sleep better if I know)
Joe.
|
I agree 100%. I finally caved in, not because of the people that kept harping
on me, but because it is part of the newsgroup FAQ. Makes about as much sense
to me as arguing about indentation and brace placement... NNTP is definitely
an outdated protocol.
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Wed Mar 03, 2004 12:25 am Post subject: Re: Defining constants in classes |
|
|
* Pete Becker <petebecker (AT) acm (DOT) org> schriebt:
| Quote: | "Alf P. Steinbach" wrote:
* "Jonathan Turkanis" <technews (AT) kangaroologic (DOT) com> schriebt:
"Joe" <anon (AT) anon (DOT) com> wrote in message
news:9Y61c.2391$XB.1720 (AT) newsfe1-gui (DOT) server.ntli.net...
This is a very basic question, but why can't I do the following ?
class schedule {
private:
static const string mScheduleFile = "schedule.txt";
...
}
The language rules only permit in-class initialization of static const
members of integral type.
Yes, but the OP asked why.
The answer to that seems to be "nobody knows why".
The language just turned out that way through historic accidents etc.
The reason is that in-class initialization of static const members was
added in order to support their use as compile-time constants.
|
Yes, the usefulness of compile-time constants was probably the reason
why they were allowed in this context.
| Quote: | Arrays of
char, floats, etc. can't be used as compile-time constants, so can't be
initialized in this way.
|
Nope, that does not explain why other constants are not allowed. I very
much doubt that GodAllah appeared in some committee meeting and declared
"Constants That Cannot Be Used As Compile Time Constants Are So Inherently
Evil That You Must Make Them More Difficult To Use". Instead, it seems
much more plausible that the committee used a heuristic on the lines of
"add only that which is absolutely critical, _or_ championed by VIP's".
|
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Wed Mar 03, 2004 12:41 am Post subject: Re: Defining constants in classes |
|
|
"Alf P. Steinbach" wrote:
| Quote: |
* Pete Becker <petebecker (AT) acm (DOT) org> schriebt:
"Alf P. Steinbach" wrote:
* "Jonathan Turkanis" <technews (AT) kangaroologic (DOT) com> schriebt:
"Joe" <anon (AT) anon (DOT) com> wrote in message
news:9Y61c.2391$XB.1720 (AT) newsfe1-gui (DOT) server.ntli.net...
This is a very basic question, but why can't I do the following ?
class schedule {
private:
static const string mScheduleFile = "schedule.txt";
...
}
The language rules only permit in-class initialization of static const
members of integral type.
Yes, but the OP asked why.
The answer to that seems to be "nobody knows why".
The language just turned out that way through historic accidents etc.
The reason is that in-class initialization of static const members was
added in order to support their use as compile-time constants.
Yes, the usefulness of compile-time constants was probably the reason
why they were allowed in this context.
|
It's not probably the reason. It is the reason.
| Quote: |
Arrays of
char, floats, etc. can't be used as compile-time constants, so can't be
initialized in this way.
Nope, that does not explain why other constants are not allowed. I very
much doubt that GodAllah appeared in some committee meeting and declared
"Constants That Cannot Be Used As Compile Time Constants Are So Inherently
Evil That You Must Make Them More Difficult To Use". Instead, it seems
much more plausible that the committee used a heuristic on the lines of
"add only that which is absolutely critical, _or_ championed by VIP's".
|
Phrase it however you like. Regardless, no historic [sic] accidents etc.
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
|
|
| Back to top |
|
 |
Jon Bell Guest
|
Posted: Wed Mar 03, 2004 12:47 am Post subject: Re: Defining constants in classes |
|
|
In article <jt71c.2411$XB.1276 (AT) newsfe1-gui (DOT) server.ntli.net>,
Joe <anon (AT) anon (DOT) com> wrote:
[snippety snippety snip]
| Quote: | Sorry for the top-post!!
(I have never understood why that is a big deal - it makes life so much
faster if you don't have to scroll, especially for a short post like this.
|
It makes life so much faster for the people who read your postings if you
*delete* quoted material that isn't immediately relevant to your response.
Then people neither have to scroll through it, nor wait for it to
download. People who actually want to read the entire preceding posting
can usually fetch it easily from their news server. (for example, all I
have to do is hit the back-arrow key.)
--
Jon Bell <jtbellm4h (AT) presby (DOT) edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
|
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Wed Mar 03, 2004 1:41 am Post subject: Re: Defining constants in classes |
|
|
* Pete Becker <petebecker (AT) acm (DOT) org> schriebt:
| Quote: | "Alf P. Steinbach" wrote:
* Pete Becker <petebecker (AT) acm (DOT) org> schriebt:
"Alf P. Steinbach" wrote:
* "Jonathan Turkanis" <technews (AT) kangaroologic (DOT) com> schriebt:
"Joe" <anon (AT) anon (DOT) com> wrote in message
news:9Y61c.2391$XB.1720 (AT) newsfe1-gui (DOT) server.ntli.net...
This is a very basic question, but why can't I do the following ?
class schedule {
private:
static const string mScheduleFile = "schedule.txt";
...
}
The language rules only permit in-class initialization of static const
members of integral type.
Yes, but the OP asked why.
The answer to that seems to be "nobody knows why".
The language just turned out that way through historic accidents etc.
The reason is that in-class initialization of static const members was
added in order to support their use as compile-time constants.
Yes, the usefulness of compile-time constants was probably the reason
why they were allowed in this context.
It's not probably the reason. It is the reason.
Arrays of
char, floats, etc. can't be used as compile-time constants, so can't be
initialized in this way.
Nope, that does not explain why other constants are not allowed. I very
much doubt that GodAllah appeared in some committee meeting and declared
"Constants That Cannot Be Used As Compile Time Constants Are So Inherently
Evil That You Must Make Them More Difficult To Use". Instead, it seems
much more plausible that the committee used a heuristic on the lines of
"add only that which is absolutely critical, _or_ championed by VIP's".
Phrase it however you like. Regardless, no historic [sic] accidents etc.
|
If you have any evidence that there was some informed, well-considered
decision to disallow e.g.
struct X
{
static double const = 1.23;
};
and
struct Y
{
static char const s[] = "disallowed for Very Good Reasons";
};
then I as well as the OP and many others would like to know the reason(s).
So spit it out, don't be shy, don't beat about the bush with incoherent
mumblings about "no historic [sic] accident" and so forth.
What, in your opinion or knowledge, makes a double value incredibly more
difficult or hazard-prone or whatever than e.g. a bool or int in struct X,
and what makes the compiler unable to translate the declaration in Y to
struct Z
{
static char const s[];
};
char const Z::s[] = "allowed for Very Good Reasons";
?
|
|
| Back to top |
|
 |
Rob Williscroft Guest
|
Posted: Wed Mar 03, 2004 4:18 am Post subject: Re: Defining constants in classes |
|
|
Joe wrote in news:9Y61c.2391$XB.1720 (AT) newsfe1-gui (DOT) server.ntli.net:
| Quote: | This is a very basic question, but why can't I do the following ?
class schedule {
private:
static const string mScheduleFile = "schedule.txt";
...
}
I hope it is clear what I am trying to do. In the member functions, I
then want to "fout.open(mScheduleFile)".
Is there way of doing this that actually works?
|
You might think you would saved some typeing if this was allowed,
but unless mScheduleFile is only used as a compile-time constant
(which is imposible, see Note below) you would also need a defenition:
struct X
{
/* declaration and initializer (not a defenition)
*/
static int const value = 10;
};
/* this is ok it compile-time usage
*/
char array[ X::value ];
/* This however requires a defenition for X::value
*/
int get_value()
{
return X::value;
}
/* A more obvious case
*/
int const &get_ref()
{
return X::value;
}
The defenition for X::value looks like this:
int const X::value;
and must (according to the Standard) appear in one and only one
translation unit (aka TU, usually a .cpp file).
Note that the only type of compile-time constant that C++ supports
are intergral ones, not float, char [], char * or std::string.
So for in class initialization of non-intergral constants to have
any value we first need to eliminate the language rule that requires
the out of class defenition.
Rob.
--
http://www.victim-prime.dsl.pipex.com/
|
|
| Back to top |
|
 |
Dietmar Kuehl Guest
|
Posted: Wed Mar 03, 2004 10:20 am Post subject: top-posting to C++ newsgroups (Was: Defining constants in cl |
|
|
"Joe" <anon (AT) anon (DOT) com> wrote:
| Quote: | (I have never understood why that is a big deal - it makes life so much
faster if you don't have to scroll, especially for a short post like this.
|
Normally, for short replies you need only a short context either. Thus,
there is not much scrolling needed anyway. The only reason to quote
something is to setup a context. Thus, why quote if you don't want people
to read it *before* reading your statements on the issue. There is a
signature around which actually brings it to the point quite well but I
don't have the exact wording. It goes something like this:
A: it makes reading things harder
Q: why is top posting bad?
| Quote: | If anyone has a good reason for why people don't like it, can they tell me?
I will sleep better if I know)
|
Well, if you top posted, you might actually sleep worse: excessive quoting
is a copyright infringement! Every article is covered by an implicit
copyright which protects the author's rights. Readers are allowed to read
things, make reasonable quotes (this is always allowed and cannot really
be prohibited as far as I know), and that's about it. Quoting texts entirely
or adding only minor own stuff to an article is generally prohibited. As is,
BTW, using any code posted in articles! Of course, more liberal copyrights
can be given at the author's discretion. I think the implicit copyright, ie.
the one given if the author does not state anything explicitly, is basically
as restrictive as a copyright can become. Note, that the copyright
legislation is international law. Also note that I'm not a lawyer: this is
what I understood from reading some stuff on copyright issues. However, I'm
pretty sure that at least the gist is right even if some details are wrong.
The issue of top-posting came up in a discussion amoung the moderators of
comp.lang.c++.moderated these days, too, although embedded into the bigger
context of overquotes. Our current policy is effectively to reject articles
on the basis of overquotes if the ratio between new material and quotes is
too bad and there seems to be no reason for that much quoting. The ratio is
an objective measure while the evaluation of whether the quoting is really
necessary is subjective, of course. Most rejections due to overquoting are
due to articles using top posting although we don't reject top posts per
se. I think there is a consensus amoung the moderators to continue rejecting
articles due to overquoting.
--
<mailto:dietmar_kuehl (AT) yahoo (DOT) com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
|
|
| Back to top |
|
 |
Stewart Gordon Guest
|
Posted: Wed Mar 03, 2004 2:04 pm Post subject: Re: Defining constants in classes |
|
|
Joe wrote:
| Quote: | This is a very basic question, but why can't I do the following ?
class schedule {
private:
static const string mScheduleFile = "schedule.txt";
...
}
I hope it is clear what I am trying to do. In the member functions, I then
want to "fout.open(mScheduleFile)".
Is there way of doing this that actually works?
|
Yes.
class schedule {
private:
static const string mScheduleFile;
};
const string schedule::mScheduleFile("schedule.txt");
--
My e-mail is valid but not my primary mailbox, aside from its being the
unfortunate victim of intensive mail-bombing at the moment. Please keep
replies on the 'group where everyone may benefit.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|