Connecting Tech Pros Worldwide Forums | Help | Site Map

getting the value of a static const member

Christopher
Guest
 
Posts: n/a
#1: Jul 19 '05
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



Bill
Guest
 
Posts: n/a
#2: Jul 19 '05

re: getting the value of a static const member


On Sun, 14 Sep 2003 06:09:53 GMT in comp.lang.c++ :

[color=blue]
>
> class BigPosInt
> {
> public:
> static const int MAX_DIGITS = 30;
> // snip...
> };
>
> the function in question:
>[/color]
[color=blue]
> BigPosInt.cpp:170: `MAX_DIGITS' undeclared (first use this function)[/color]

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;

--
Regards...
Bill

Percy
Guest
 
Posts: n/a
#3: Jul 19 '05

re: getting the value of a static const member


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

[color=blue]
> On Sun, 14 Sep 2003 06:09:53 GMT in comp.lang.c++ :
>
>[color=green]
> >[/color][/color]
[color=blue][color=green]
> > BigPosInt.cpp:170: `MAX_DIGITS' undeclared (first use this function)[/color]
>
> Your compiler is wrong.... as a work around define it
> outside the class declaration, see if it'll fly that way.[/color]

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




--
Regards...
Bill

David White
Guest
 
Posts: n/a
#4: Jul 19 '05

re: getting the value of a static const member


"Percy" <Percy@noemail.given> wrote in message
news:MPG.19cde73cc7a1f302989682@news.prodigy.net.. .[color=blue]
> 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)[/color]

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



David White
Guest
 
Posts: n/a
#5: Jul 19 '05

re: getting the value of a static const member


"David White" <no.email@provided> wrote in message
news:K8Y8b.2983$d6.144033@nasal.pacific.net.au...[color=blue]
> I thought this was one of the few additions to the standard that[/color]
Stroustrup[color=blue]
> disagreed with, and it's no wonder.[/color]

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

DW



Rob Williscroft
Guest
 
Posts: n/a
#6: Jul 19 '05

re: getting the value of a static const member


Percy wrote in news:MPG.19cde73cc7a1f302989682@news.prodigy.net:
[color=blue]
> On Sun, 14 Sep 2003 07:48:25 GMT in comp.lang.c++ :
>
>[color=green]
>> On Sun, 14 Sep 2003 06:09:53 GMT in comp.lang.c++ :
>>
>>[color=darkred]
>> >[/color][/color]
>[color=green][color=darkred]
>> > BigPosInt.cpp:170: `MAX_DIGITS' undeclared (first use this function)[/color]
>>
>> Your compiler is wrong.... as a work around define it
>> outside the class declaration, see if it'll fly that way.[/color]
>
> 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....
>[/color]

You may want to see:

http://anubis.dkuug.dk/jtc1/sc22/wg2...efects.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.
--
http://www.victim-prime.dsl.pipex.com/
Christopher
Guest
 
Posts: n/a
#7: Jul 19 '05

re: getting the value of a static const member



"David White" <no.email@provided> wrote in message
news:K8Y8b.2983$d6.144033@nasal.pacific.net.au...[color=blue]
> "Percy" <Percy@noemail.given> wrote in message
> news:MPG.19cde73cc7a1f302989682@news.prodigy.net.. .[color=green]
> > 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)[/color]
>
> 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[/color]
Stroustrup[color=blue]
> disagreed with, and it's no wonder.
>
> DW[/color]

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


Pete Becker
Guest
 
Posts: n/a
#8: Jul 19 '05

re: getting the value of a static const member


Christopher wrote:[color=blue]
>
> 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?[/color]

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.
[color=blue]
> Oh and my instructor wont let me use a #define because he is
> a terd.
>[/color]

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

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Christopher
Guest
 
Posts: n/a
#9: Jul 19 '05

re: getting the value of a static const member



"Pete Becker" <petebecker@acm.org> wrote in message
news:3F646E5B.3256B42@acm.org...[color=blue]
> Christopher wrote:[color=green]
> >
> > I could of sworn you could get the value of a static member of a class[/color][/color]
using[color=blue][color=green]
> > this syntax "class::member" but my compliler is complaining. How can I[/color][/color]
do[color=blue][color=green]
> > what I am trying to do here besides making a function for it. Isnt there[/color][/color]
a[color=blue][color=green]
> > short easy way?[/color]
>
> 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.[/color]

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.
[color=blue][color=green]
> > Oh and my instructor wont let me use a #define because he is
> > a terd.
> >[/color]
>
> Lose the attitude. It's quite likely that your instructor knows more
> about what he's trying to accomplish than you do.[/color]

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.
[color=blue]
> --
>
> Pete Becker
> Dinkumware, Ltd. (http://www.dinkumware.com)[/color]


jeffc
Guest
 
Posts: n/a
#10: Jul 19 '05

re: getting the value of a static const member



"Christopher" <cpisz@austin.rr.com> wrote in message
news:RCT8b.14953$KW1.9589@twister.austin.rr.com...[color=blue]
> I could of sworn...[/color]

You could HAVE sworn...
[color=blue]
>... you could get the value of a static member of a class using
> this syntax "class::member" but my compliler is complaining.[/color]

Let me guess, Visual C++?
[color=blue]
> Oh and my instructor wont let me use a #define because he is a terd.[/color]

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


Pete Becker
Guest
 
Posts: n/a
#11: Jul 19 '05

re: getting the value of a static const member


jeffc wrote:[color=blue]
>
> "Christopher" <cpisz@austin.rr.com> wrote in message
> news:RCT8b.14953$KW1.9589@twister.austin.rr.com...[color=green]
> > I could of sworn...[/color]
>
> You could HAVE sworn...
>[color=green]
> >... you could get the value of a static member of a class using
> > this syntax "class::member" but my compliler is complaining.[/color]
>
> Let me guess, Visual C++?
>[/color]

Probably not. VC++ complains at the point of definition.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Closed Thread