consts in classes

C

Christopher

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

Victor Bazarov

Christopher said:
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
 
C

Christopher

In any single translation unit, since your static const is not of
the integral type.


It doesn't mind them because they are most likely of an integral type.






Remove initialisers from both lines, they are not allowed.




Add right here:

const double X::TESTER1 = 0.1;
const double X::TESTER2 = 0.2;






Sure. Static objects need to be defined.

V


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

Victor Bazarov

Christopher said:
[..] 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
 
A

Andre Kostur

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

Kira Yamato

Christopher said:
[..] 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.
 
J

James Kanze

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

James Kanze

[..] 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.
[/QUOTE]
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top