Declaring constants within the scope of a class

  • Thread starter Generic Usenet Account
  • Start date
G

Generic Usenet Account

I am trying to compile the following sample code:

class WhatISHappeningHere
{
static const int x = 32;
static const char* yy = "Howdy";
// ...
// blah blah blah
// ...
};


My GCC compiler (g++ version 2.95.2) is giving the following misleading
compiler error:

ANSI C++ forbids in-class initialization of non-const static member
`yy'


I suspect that initialization of static data members that are not of an
integral type is not allowed in C++, but the compiler is giving a wrong
error message. Kindly confirm/refute my observation.

Thanks,
Gus
 
S

Shezan Baig

Generic said:
I am trying to compile the following sample code:

class WhatISHappeningHere
{
static const int x = 32;
static const char* yy = "Howdy";
// ...
// blah blah blah
// ...
};


My GCC compiler (g++ version 2.95.2) is giving the following misleading
compiler error:

ANSI C++ forbids in-class initialization of non-const static member
`yy'


I suspect that initialization of static data members that are not of an
integral type is not allowed in C++, but the compiler is giving a wrong
error message. Kindly confirm/refute my observation.

Thanks,
Gus



I'm not sure if this will work, but try:

static const char* const yy = "Howdy";
 
H

Howard

Shezan Baig said:
I'm not sure if this will work, but try:

static const char* const yy = "Howdy";

That won't work, either. As the OP thought, you're not allowed to
initialize static data members in the class definition, unless they're of
integral type. They need to be initialized outside the class definition
(and outside the header file, if you're using one, to avoid multiple
definitions).

Like you, I was thinking that the reason the compiler is saying that's
"non-const" is that it's a const ponter, but to non-const data. But making
it const data won't help here. It has to be an integral type to be
initialized "in class".

-Howard
 
V

Victor Bazarov

'yy' is definitely non-const. It would be 'const' if "const" were
sitting right in front of him. See the declaration below (which is
not going to work anyway).

Seems like you're incorrect. However, the fact that you got confused
by the message may allow qualifying the message as 'confusing', but not
misleading.
I'm not sure if this will work, but try:

static const char* const yy = "Howdy";

This will NOT work. You are only allowed to initialise static const
members of _integral_ types.

V
 
V

Victor Bazarov

Howard said:
[..] As the OP thought, you're not allowed to
initialize static data members

....static _const_ data members...

(Just to pick a nit)
in the class definition, unless they're of
integral type. They need to be initialized outside the class definition
(and outside the header file, if you're using one, to avoid multiple
definitions).
[..]


V
 
H

Howard

Victor Bazarov said:
Howard said:
[..] As the OP thought, you're not allowed to initialize static data
members

...static _const_ data members...

(Just to pick a nit)
in the class definition, unless they're of
integral type. They need to be initialized outside the class definition
(and outside the header file, if you're using one, to avoid multiple
definitions).
[..]


Quite correct. Thanks. (I hate those nits!)

-Howard
 

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,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top