473,511 Members | 13,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

consts in classes

Where is the proper place to assign the value to a public static const
variable that is a member of a class?

My compiler is angry with me, but I don't understand why. It doesn't
mind any other static consts that I have declared in the same manner.

// x.h

class X
{
public:

static const double TESTER1 = 0.1;
static const double TESTER2 = 0.2;

X(const SomeClass & rhs);

};

// x.cpp

#include "x.h"

X::X(const SomeClass & rhs)
{
int g = TESTER1;
int h = TESTER2;
}

Compiler complains about undefined referance to TESTER2 when the copy
constuctor is called from another library that is dependant on the
library that contains class X.

Nov 8 '07 #1
7 1472
Christopher wrote:
Where is the proper place to assign the value to a public static const
variable that is a member of a class?
In any single translation unit, since your static const is not of
the integral type.
My compiler is angry with me, but I don't understand why. It doesn't
mind any other static consts that I have declared in the same manner.
It doesn't mind them because they are most likely of an integral type.
>
// x.h

class X
{
public:

static const double TESTER1 = 0.1;
static const double TESTER2 = 0.2;
Remove initialisers from both lines, they are not allowed.
>
X(const SomeClass & rhs);

};

// x.cpp
Add right here:

const double X::TESTER1 = 0.1;
const double X::TESTER2 = 0.2;
>
#include "x.h"

X::X(const SomeClass & rhs)
{
int g = TESTER1;
int h = TESTER2;
}

Compiler complains about undefined referance to TESTER2 when the copy
constuctor is called from another library that is dependant on the
library that contains class X.
Sure. Static objects need to be defined.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 8 '07 #2
On Nov 8, 2:54 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Christopher wrote:
Where is the proper place to assign the value to a public static const
variable that is a member of a class?

In any single translation unit, since your static const is not of
the integral type.
My compiler is angry with me, but I don't understand why. It doesn't
mind any other static consts that I have declared in the same manner.

It doesn't mind them because they are most likely of an integral type.
// x.h
class X
{
public:
static const double TESTER1 = 0.1;
static const double TESTER2 = 0.2;

Remove initialisers from both lines, they are not allowed.
X(const SomeClass & rhs);
};
// x.cpp

Add right here:

const double X::TESTER1 = 0.1;
const double X::TESTER2 = 0.2;
#include "x.h"
X::X(const SomeClass & rhs)
{
int g = TESTER1;
int h = TESTER2;
}
Compiler complains about undefined referance to TESTER2 when the copy
constuctor is called from another library that is dependant on the
library that contains class X.

Sure. Static objects need to be defined.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

Did the trick. I was under the impression that the standard now
allowed us to define constants inside class definitions though. Of
course, there is no telling if my compiler agrees, so this solution is
fine by me.

Nov 8 '07 #3
Christopher wrote:
[..] I was under the impression that the standard now
allowed us to define constants inside class definitions though.
As I said before, only for integral types.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 8 '07 #4
Christopher <cp***@austin.rr.comwrote in news:1194553765.220446.233130
@i13g2000prf.googlegroups.com:
Where is the proper place to assign the value to a public static const
variable that is a member of a class?

My compiler is angry with me, but I don't understand why. It doesn't
mind any other static consts that I have declared in the same manner.

// x.h

class X
{
public:

static const double TESTER1 = 0.1;
static const double TESTER2 = 0.2;
IIRC, you can only do integral constants inside the class declaration.
Drop the "= 0.1" (and 0.2) parts.
>
X(const SomeClass & rhs);

};

// x.cpp

#include "x.h"
Add here:

const double X::TESTER1 = 0.1;
const double X::TESTER2 = 0.2;
X::X(const SomeClass & rhs)
{
int g = TESTER1;
int h = TESTER2;
}

Compiler complains about undefined referance to TESTER2 when the copy
constuctor is called from another library that is dependant on the
library that contains class X.

Nov 8 '07 #5
On 2007-11-08 16:16:08 -0500, "Victor Bazarov" <v.********@comAcast.netsaid:
Christopher wrote:
>[..] I was under the impression that the standard now
allowed us to define constants inside class definitions though.

As I said before, only for integral types.

V
Kind of a silly rule to have that only the native type for integers can
be initialized in class declarations.

--

-kira

Nov 9 '07 #6
On Nov 8, 10:00 pm, Christopher <cp...@austin.rr.comwrote:
Did the trick. I was under the impression that the standard now
allowed us to define constants inside class definitions though. Of
course, there is no telling if my compiler agrees, so this solution is
fine by me.
It's a hack.

The general rule is that you can only define an initializer in a
definition, and that a static data member of a class is a
declaration, not a definition.

A special exception was made for integral static data members,
because there are special places where the compiler requires
integral constant expressions, and to be an integral constant
expression, the initializer must be visible. But it remains a
special exception, and a violation of the basic principles of
the language. In sum, a hack.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 9 '07 #7
On Nov 9, 2:08 am, Kira Yamato <kira...@earthlink.netwrote:
On 2007-11-08 16:16:08 -0500, "Victor Bazarov" <v.Abaza...@comAcast.netsaid:
Christopher wrote:
[..] I was under the impression that the standard now
allowed us to define constants inside class definitions though.
As I said before, only for integral types.
Kind of a silly rule to have that only the native type for
integers can be initialized in class declarations.
It's a major inconsistency with the principles of the language
to allow any initializers on a declaration which is not a
definition. Integral constant expressions, however, play a
special role in the language, at least in certain contexts, so
it was deemed preferable to be inconsistent in this one case.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Nov 9 '07 #8

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

Similar topics

1
741
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
4
2820
by: trying_to_learn | last post by:
I'm learning consts in C++ and the book says that u have to initialize non-static consts inside the constructor initializer list, however "const string* stack" isn't initialized in the constructor...
2
9479
by: joye | last post by:
Hello, My question is how to use C# to call the existing libraries containing unmanaged C++ classes directly, but not use C# or managed C++ wrappers unmanaged C++ classes? Does anyone know how...
18
2018
by: Edward Diener | last post by:
Is the packing alignment of __nogc classes stored as part of the assembly ? I think it must as the compiler, when referencing the assembly, could not know how the original data is packed otherwise....
4
1535
by: Oliver S | last post by:
I got a class, a VCL form actually. I have two const member that I am initializing (in the order they were declared). My question is, do I put them before calling inherited constructor or after? ...
4
2068
by: Chukkalove | last post by:
Hi I have an abstract class that contains 100% static methods and variables. One of the member variables "string DatabaseName" needs to be overridden in derived classes. Am I able to keep my...
6
2919
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
0
2017
by: ivan.leben | last post by:
I am writing this in a new thread to alert that I found a solution to the problem mentioned here: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/7970afaa089fd5b8 and to avoid...
5
1495
by: Jon | last post by:
Hello all, Am I missing something quite simple as I can't seem to see how to inherit consts from my base class. Can this be done? Thanks, Jon
0
7251
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,...
0
7367
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,...
0
7430
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...
0
7517
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...
0
5673
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,...
1
5072
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3217
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
790
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.