getting the value of a static const member

C

Christopher

I could of sworn you could get the value of a static member of a class using
this syntax "class::member" but my compliler is complaining. How can I do
what I am trying to do here besides making a function for it. Isnt there a
short easy way? Oh and my instructor wont let me use a #define because he is
a terd.

the header:

class BigPosInt
{
public:
static const int MAX_DIGITS = 30;
// snip...
};

the function in question:

void input(istream& ins, BigPosInt& targetBpi)
{
int counter = 0;
char one_byte;
int buffer[BigPosInt::MAX_DIGITS];
// snip..
}

the error:
BigPosInt.cpp:170: `MAX_DIGITS' undeclared (first use this function)

Thanx,
Christopher
 
B

Bill

class BigPosInt
{
public:
static const int MAX_DIGITS = 30;
// snip...
};

the function in question:
BigPosInt.cpp:170: `MAX_DIGITS' undeclared (first use this function)

Your compiler is wrong.... as a work around define it
outside the class declaration, see if it'll fly that way.

(I.e.)
const int BigPosInt::MAX_DIGITS = 30;
 
P

Percy

Your compiler is wrong.... as a work around define it
outside the class declaration, see if it'll fly that way.

Actually I am wrong, just browsed the standard.
A static const data member can be initialized in the
declaration (integral type), but if it used "in" the
program "it shall" still be defined in namespace scope"
and the definition "shall not" contain an initializer.
(9.4.2-4)

so the way I read it ;

class A {
public:
static const int AINT = 30;// initialized and declared
};
// this case global namespace
const int A::AINT; // defined ?

Apologize for the bad advice....
 
D

David White

Percy said:
On Sun, 14 Sep 2003 07:48:25 GMT in comp.lang.c++ :

Actually I am wrong, just browsed the standard.
A static const data member can be initialized in the
declaration (integral type), but if it used "in" the
program "it shall" still be defined in namespace scope"
and the definition "shall not" contain an initializer.
(9.4.2-4)

If I understand this correctly, it is pointless to make such a member
public. You are quite welcome to place the initializer in the class
definition. Just don't use it in the program.

I thought this was one of the few additions to the standard that Stroustrup
disagreed with, and it's no wonder.

DW
 
D

David White

David White said:
I thought this was one of the few additions to the standard that Stroustrup
disagreed with, and it's no wonder.

Allowing the initializer in the class definition at all, I meant, not the
conditions on its use.

DW
 
R

Rob Williscroft

Percy wrote in
Actually I am wrong, just browsed the standard.
A static const data member can be initialized in the
declaration (integral type), but if it used "in" the
program "it shall" still be defined in namespace scope"
and the definition "shall not" contain an initializer.
(9.4.2-4)

so the way I read it ;

class A {
public:
static const int AINT = 30;// initialized and declared
};
// this case global namespace
const int A::AINT; // defined ?

Apologize for the bad advice....

You may want to see:

http://anubis.dkuug.dk/jtc1/sc22/wg21/docs/cwg_defects.html#48

issue 48. Definitions of unused static members

This issue has status TC1 so IIUC its resolution to allow the
OP's original code is Standard C++.

Rob.
 
C

Christopher

David White said:
If I understand this correctly, it is pointless to make such a member
public. You are quite welcome to place the initializer in the class
definition. Just don't use it in the program.

I thought this was one of the few additions to the standard that Stroustrup
disagreed with, and it's no wonder.

DW

I would of done things much differantly, but this is my instructors header
file and I am not allowed to change it at all, the whole thing is done very
badly.
,
Chris
 
P

Pete Becker

Christopher said:
I could of sworn you could get the value of a static member of a class using
this syntax "class::member" but my compliler is complaining. How can I do
what I am trying to do here besides making a function for it. Isnt there a
short easy way?

There's nothing wrong with the code you posted. So either your compiler
doesn't support this properly or the code you posted is different from
the actual code.
Oh and my instructor wont let me use a #define because he is
a terd.

Lose the attitude. It's quite likely that your instructor knows more
about what he's trying to accomplish than you do.
 
C

Christopher

Pete Becker said:
There's nothing wrong with the code you posted. So either your compiler
doesn't support this properly or the code you posted is different from
the actual code.

Found that the :: operation works fine on another compiler. You are correct.
Frustrating how one feature works on one compiler, but another feature does
not.
Lose the attitude. It's quite likely that your instructor knows more
about what he's trying to accomplish than you do.

Well I would give a list of reasons why the attitude was developed, but I
hardly think anyone cares. Let's just say that I am sure I am justified in
my opinion.
 
J

jeffc

Christopher said:
I could of sworn...

You could HAVE sworn...
... you could get the value of a static member of a class using
this syntax "class::member" but my compliler is complaining.

Let me guess, Visual C++?
Oh and my instructor wont let me use a #define because he is a terd.

He is a TURD, not terd. And that remains to be seen. I wouldn't let you
use #define either.
 

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

Forum statistics

Threads
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top