473,698 Members | 2,521 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Defining constants in classes

Joe
This is a very basic question, but why can't I do the following ?

class schedule {

private:
static const string mScheduleFile = "schedule.t xt";
....
}
I hope it is clear what I am trying to do. In the member functions, I then
want to "fout.open(mSch eduleFile)".
Is there way of doing this that actually works?

TIA,
Joe.
Jul 22 '05 #1
29 4630

"Joe" <an**@anon.co m> wrote in message
news:9Y******** ********@newsfe 1-gui.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.t xt";
...
}


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.t xt";
return file;
}
Jonathan
Jul 22 '05 #2
* "Jonathan Turkanis" <te******@kanga roologic.com> schriebt:

"Joe" <an**@anon.co m> wrote in message
news:9Y******** ********@newsfe 1-gui.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.t xt";
...
}

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.t xt";
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::mSche duleFile = "schedule.t xt";
Jul 22 '05 #3
Joe
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" <te******@kanga roologic.com> wrote in message
news:c2******** *****@ID-216073.news.uni-berlin.de...

"Joe" <an**@anon.co m> wrote in message
news:9Y******** ********@newsfe 1-gui.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.t xt";
...
}


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.t xt";
return file;
}
Jonathan

Jul 22 '05 #4
Joe
"Joe" <an**@anon.co m> wrote in message
news:Kn******** *******@newsfe1-gui.server.ntli .net...
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" <te******@kanga roologic.com> wrote in message
news:c2******** *****@ID-216073.news.uni-berlin.de...

"Joe" <an**@anon.co m> wrote in message
news:9Y******** ********@newsfe 1-gui.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.t xt";
...
}


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.t xt";
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.
Jul 22 '05 #5
"Joe" <an**@anon.co m> wrote in message
news:Kn******** *******@newsfe1-gui.server.ntli .net...
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
Jul 22 '05 #6
"Alf P. Steinbach" wrote:

* "Jonathan Turkanis" <te******@kanga roologic.com> schriebt:

"Joe" <an**@anon.co m> wrote in message
news:9Y******** ********@newsfe 1-gui.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.t xt";
...
}


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)
Jul 22 '05 #7
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.
Jul 22 '05 #8
* Pete Becker <pe********@acm .org> schriebt:
"Alf P. Steinbach" wrote:

* "Jonathan Turkanis" <te******@kanga roologic.com> schriebt:

"Joe" <an**@anon.co m> wrote in message
news:9Y******** ********@newsfe 1-gui.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.t xt";
> ...
> }
>

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

Jul 22 '05 #9
"Alf P. Steinbach" wrote:

* Pete Becker <pe********@acm .org> schriebt:
"Alf P. Steinbach" wrote:

* "Jonathan Turkanis" <te******@kanga roologic.com> schriebt:
>
> "Joe" <an**@anon.co m> wrote in message
> news:9Y******** ********@newsfe 1-gui.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.t xt";
> > ...
> > }
> >
>
> 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)
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3687
by: Christian Hackl | last post by:
I honestly wasn't able to find an answer for this design question using Google and Google Groups, so I apologize if it is asked too frequently :) Anyway: Let's say I have a multidimensional array of the following kind: $people = array(); // maps age and e-mail address to names $people = array(21, "paul@foo.bar"); $people = array(22, "linda@bar.foo"); $people = array(19, "max@foobar.foobar");
2
25452
by: Sriram Chadalavada | last post by:
Hello everyone, I am a newbie to Python with experience in C programming. For my project, I am re-writing C routines as Python functions. I am currently using raw numerical values and was wondering if there is an equivalent of #define in Python for declaring constants. Is there an elegant way of defining constants in Python? Please let me know. Thanks, Sriram
38
8705
by: Ted | last post by:
Is there a way to define a constant value for a color in a CSS declaration? I have numerous colors in different CSS elements, and if I make a change in color, I have to change all the reference to the color. For example: Instead of this: color: #306090 I would like to say: define MyShadeOfBlue = '#306090'
11
7776
by: Bill Nguyen | last post by:
I need to make a set of constants to be available for the whole application. Public const is confined to the form from which constants are declared. Is there a way to declare once and all forms can access the constant values? Thanks Bill
6
3125
by: PC | last post by:
Gentlesofts, Forgive me. I'm an abject newbie in your world, using VB 2005 with the dot-Net wonderfulness. So, I'm writing a wonderful class or two to interface with a solemnly ancient database. In times recently past, I would have done this with Borland Delphi. So, that's my perspective and I have my old Delphi code to crib from. That seems good.
6
1722
by: alan | last post by:
I'm creating a sort-of "wrapper" class which (partly) acts like a variable. Something like: template<class t> class cell{ t curval; public: /*public for debugging only - will be private in final version*/ inline cell<t>& set_value(t v){ curval = v; return *this;} inline t get_value(){ return curval;} /*actual public interface*/
3
1801
by: boris | last post by:
Hi. If somebody can help out with this design question, thank you very much. Suppose I have a global constant, like an images directory. This constant will be referenced from multiple classes. How do I define this constant only once while using OOP? I could define the constant on one of the classes and then reference it with ClassName::const from the other classes, but this ties the constants to one class arbitrarily. In Java you can...
12
6125
by: Gordon | last post by:
I want to provide a set of static functions in a superclass that work with class constants defined in a decendant of that class. Unfortunately I've run into a snag with this idea. Example: class SuperClass { const CNST = 'Super class'; public static function getCnst () {
54
3598
by: shuisheng | last post by:
Dear All, I am always confused in using constants in multiple files. For global constants, I got some clues from http://msdn.microsoft.com/en-us/library/0d45ty2d(VS.80).aspx So in header file writing: const double PI = 3.14; Every time using it, include the header file.
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8904
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7741
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.