473,788 Members | 3,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static const Class Member

Hi,

what's the usual way in C++ to define a static constant class member?
Consider the following example:

class TestClass
{
public:
static const double a = 12.5;
static const double b = 2 * a;
};

While g++-3.2 is totally fine with this, g++-4.1.1 gives an error:
test.cpp:5: error: ‘TestClass::a ’ cannot appear in a constant-expression

I can't see where's the problem here. TestClass::a is declared as being
constant and so is TestClass::b. I know that another possible way (that
also g++-4.1.1 accepts) is to initialize the values outside the class
definition:

class TestClass
{
public:
static const double a;
static const double b;
};

const double TestClass::a = 12.5;
const double TestClass::b = 2 * TestClass::a;

However, I shouldn't put the last two lines into a header file if it is
included by more than one .cpp implementation files if the resulting object
files are supposed to be linked afterwards, since there will be multiple
initializations then, no matter if they are the same or not. Putting the
initialization into one specific implementation file, everything will work,
but I would like to have the initialization within the header file.

Anyhow, I'm most interested in an explanation of the reason why g++-4.1.1 is
telling me why TestClass::a can't appear in a constant-expression.
Best regards,

Alex

Sep 6 '07 #1
3 2670
Alexander Hans wrote:
Hi,

what's the usual way in C++ to define a static constant class member?
Consider the following example:

class TestClass
{
public:
static const double a = 12.5;
static const double b = 2 * a;
only integral type can't be initialized inside of class declaration.
};

While g++-3.2 is totally fine with this, g++-4.1.1 gives an error:
>test.cpp:5: error: ‘TestClass::a ’ cannot appear in a constant-expression


I can't see where's the problem here. TestClass::a is declared as being
constant and so is TestClass::b. I know that another possible way (that
also g++-4.1.1 accepts) is to initialize the values outside the class
definition:

class TestClass
{
public:
static const double a;
static const double b;
};

const double TestClass::a = 12.5;
const double TestClass::b = 2 * TestClass::a;

However, I shouldn't put the last two lines into a header file if it is
included by more than one .cpp implementation files if the resulting object
you need to place them in a cxx file.
files are supposed to be linked afterwards, since there will be multiple
initializations then, no matter if they are the same or not. Putting the
initialization into one specific implementation file, everything will work,
but I would like to have the initialization within the header file.

Anyhow, I'm most interested in an explanation of the reason why g++-4.1.1 is
telling me why TestClass::a can't appear in a constant-expression.
Best regards,

Alex

--
Thanks
Barry
Sep 6 '07 #2
On Sep 6, 7:53 am, Barry <dhb2...@gmail. comwrote:
Alexander Hans wrote:
Hi,
what's the usual way in C++ to define a static constant class member?
Consider the following example:
class TestClass
{
public:
static const double a = 12.5;
static const double b = 2 * a;

only integral type can't be initialized inside of class declaration.


};
While g++-3.2 is totally fine with this, g++-4.1.1 gives an error:
test.cpp:5: error: 'TestClass::a' cannot appear in a constant-expression
I can't see where's the problem here. TestClass::a is declared as being
constant and so is TestClass::b. I know that another possible way (that
also g++-4.1.1 accepts) is to initialize the values outside the class
definition:
class TestClass
{
public:
static const double a;
static const double b;
};
const double TestClass::a = 12.5;
const double TestClass::b = 2 * TestClass::a;
However, I shouldn't put the last two lines into a header file if it is
included by more than one .cpp implementation files if the resulting object

you need to place them in a cxx file.
files are supposed to be linked afterwards, since there will be multiple
initializations then, no matter if they are the same or not. Putting the
initialization into one specific implementation file, everything will work,
but I would like to have the initialization within the header file.
Anyhow, I'm most interested in an explanation of the reason why g++-4.1.1 is
telling me why TestClass::a can't appear in a constant-expression.
Best regards,
Alex

--
Thanks
Barry- Hide quoted text -

- Show quoted text -
I think Barry meant to say only integral types CAN be initialized
inside a class declaration. This includes char, short, int, long int,
long long and enum types. It does _NOT_ include float, double or long
double.

Sep 6 '07 #3
blangela wrote:
On Sep 6, 7:53 am, Barry <dhb2...@gmail. comwrote:
>Alexander Hans wrote:
>>Hi,
what's the usual way in C++ to define a static constant class member?
Consider the following example:
class TestClass
{
public:
static const double a = 12.5;
static const double b = 2 * a;
only integral type can't be initialized inside of class declaration.


>>};
While g++-3.2 is totally fine with this, g++-4.1.1 gives an error:
test.cpp:5 : error: 'TestClass::a' cannot appear in a constant-expression
I can't see where's the problem here. TestClass::a is declared as being
constant and so is TestClass::b. I know that another possible way (that
also g++-4.1.1 accepts) is to initialize the values outside the class
definition:
class TestClass
{
public:
static const double a;
static const double b;
};
const double TestClass::a = 12.5;
const double TestClass::b = 2 * TestClass::a;
However, I shouldn't put the last two lines into a header file if it is
included by more than one .cpp implementation files if the resulting object
you need to place them in a cxx file.
>>files are supposed to be linked afterwards, since there will be multiple
initializatio ns then, no matter if they are the same or not. Putting the
initializatio n into one specific implementation file, everything will work,
but I would like to have the initialization within the header file.
Anyhow, I'm most interested in an explanation of the reason why g++-4.1.1 is
telling me why TestClass::a can't appear in a constant-expression.
Best regards,
Alex
--
Thanks
Barry- Hide quoted text -

- Show quoted text -

I think Barry meant to say only integral types CAN be initialized
inside a class declaration. This includes char, short, int, long int,
long long and enum types. It does _NOT_ include float, double or long
double.
Yep,
sorry, kind of merge "you can't ...", "..." together. :-)

--
Thanks
Barry
Sep 7 '07 #4

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

Similar topics

5
2063
by: Thomas Matthews | last post by:
Hi, I have three classes: Category Author Publisher Each of these classes stores its information into a database table of <ID, text>. (They are fields that have a foreign key.) There is only one table for each instance of these classes (IOW,
3
3614
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming non-standard C++ or if the problem lies elsewhere. To summarize static const class members are not being accessed properly when accessed from a DLL by another external object file (not within the DLL). It only occurs when the static const...
14
2872
by: Mike Hewson | last post by:
Have been researching as to why: <example 1> class ABC { static const float some_float = 3.3f; }; <end example 1>
8
2041
by: Srini | last post by:
Hello all, I was just wondering about this. A const member function guarantees constness of the object within the function body. But there's no way for a member function to guarantee the constness of static members. Is there a way to do that? Also, is there a way for a static member function to guarantee constness of static members? TIA Regards,
5
3610
by: mast2as | last post by:
Hi guys Here's the class I try to compile (see below). By itself when I have a test.cc file for example that creates an object which is an instance of the class SpectralProfile, it compiles fine. 1 / Now If I move the getSpectrumAtDistance( const T &dist ) method definition in SpectralProfofile.cc let's say the compiler says core/profile.cc:199: error: `kNumBins' was not declared in this scope
5
11326
by: John Goche | last post by:
Hello, I would like to know whethere there is a difference between a const variable and a static const variable inside a class. After all, if a variable is const in a class, the compiler can always optimize and turn it into a static const variable to save runtim memory/stack space since being const it cannot be changed. (?) Thanks,
8
8932
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it. Like, for instance the Objective-C method: +(void)initialize Which has the following characteristics: It is guaranteed to be run
9
8896
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've *defined* "x"'s value to be 10, isn't above statement a
15
7873
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
4
5824
by: aaragon | last post by:
Hi everyone, I have a linking error when using gcc4.2 and static member variables. The class template definition is something around the following: template<> class Element<L2_t: public Element_common<L2, Side<2,2 { public:
0
9655
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10363
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...
1
10110
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
9964
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
8993
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
5535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
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
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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.