Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Defining constants in classes

Reply
Thread Tools

Defining constants in classes

 
 
Joe
Guest
Posts: n/a
 
      03-02-2004
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.


 
Reply With Quote
 
 
 
 
Jonathan Turkanis
Guest
Posts: n/a
 
      03-02-2004

"Joe" <(E-Mail Removed)> wrote in message
news:9Y61c.2391$(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-02-2004
* "Jonathan Turkanis" <(E-Mail Removed)> schriebt:
>
> "Joe" <(E-Mail Removed)> wrote in message
> news:9Y61c.2391$(E-Mail Removed)...
> > 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.




> 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";


 
Reply With Quote
 
Joe
Guest
Posts: n/a
 
      03-02-2004
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" <(E-Mail Removed)> wrote in message
news:c22uv5$1oqo3p$(E-Mail Removed)-berlin.de...
>
> "Joe" <(E-Mail Removed)> wrote in message
> news:9Y61c.2391$(E-Mail Removed)...
> > 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
>
>



 
Reply With Quote
 
Joe
Guest
Posts: n/a
 
      03-02-2004
"Joe" <(E-Mail Removed)> wrote in message
news:Kn71c.2406$(E-Mail Removed)...
> 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" <(E-Mail Removed)> wrote in message
> news:c22uv5$1oqo3p$(E-Mail Removed)-berlin.de...
> >
> > "Joe" <(E-Mail Removed)> wrote in message
> > news:9Y61c.2391$(E-Mail Removed)...
> > > 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.


 
Reply With Quote
 
Jonathan Turkanis
Guest
Posts: n/a
 
      03-02-2004
"Joe" <(E-Mail Removed)> wrote in message
news:Kn71c.2406$(E-Mail Removed)...
> 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


 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      03-02-2004
"Alf P. Steinbach" wrote:
>
> * "Jonathan Turkanis" <(E-Mail Removed)> schriebt:
> >
> > "Joe" <(E-Mail Removed)> wrote in message
> > news:9Y61c.2391$(E-Mail Removed)...
> > > 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)
 
Reply With Quote
 
Julie
Guest
Posts: n/a
 
      03-02-2004
Joe wrote:
> 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.
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      03-03-2004
* Pete Becker <(E-Mail Removed)> schriebt:
> "Alf P. Steinbach" wrote:
> >
> > * "Jonathan Turkanis" <(E-Mail Removed)> schriebt:
> > >
> > > "Joe" <(E-Mail Removed)> wrote in message
> > > news:9Y61c.2391$(E-Mail Removed)...
> > > > 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.


> 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".

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      03-03-2004
"Alf P. Steinbach" wrote:
>
> * Pete Becker <(E-Mail Removed)> schriebt:
> > "Alf P. Steinbach" wrote:
> > >
> > > * "Jonathan Turkanis" <(E-Mail Removed)> schriebt:
> > > >
> > > > "Joe" <(E-Mail Removed)> wrote in message
> > > > news:9Y61c.2391$(E-Mail Removed)...
> > > > > 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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Defining Constants Gene Wirchenko Javascript 1 12-16-2011 10:45 PM
defining or not defining destructors johny smith C++ 8 07-02-2004 08:51 AM
Defining classes in jscript Henke ASP .Net 3 11-28-2003 04:18 PM
Defining constants in global scope Thomas Sondergaard Ruby 2 09-24-2003 06:02 AM
Defining/declaring constants in Python Sriram Chadalavada Python 2 07-29-2003 11:56 AM



Advertisments