Connecting Tech Pros Worldwide Help | Site Map

getting the value of a static const member

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 19th, 2005, 05:30 PM
Christopher
Guest
 
Posts: n/a
Default getting the value of a static const member

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



  #2  
Old July 19th, 2005, 05:30 PM
Bill
Guest
 
Posts: n/a
Default 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

  #3  
Old July 19th, 2005, 05:30 PM
Percy
Guest
 
Posts: n/a
Default 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

  #4  
Old July 19th, 2005, 05:30 PM
David White
Guest
 
Posts: n/a
Default 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



  #5  
Old July 19th, 2005, 05:30 PM
David White
Guest
 
Posts: n/a
Default 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



  #6  
Old July 19th, 2005, 05:30 PM
Rob Williscroft
Guest
 
Posts: n/a
Default 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/
  #7  
Old July 19th, 2005, 05:30 PM
Christopher
Guest
 
Posts: n/a
Default 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


  #8  
Old July 19th, 2005, 05:30 PM
Pete Becker
Guest
 
Posts: n/a
Default 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)
  #9  
Old July 19th, 2005, 05:31 PM
Christopher
Guest
 
Posts: n/a
Default 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]


  #10  
Old July 19th, 2005, 05:33 PM
jeffc
Guest
 
Posts: n/a
Default 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.


  #11  
Old July 19th, 2005, 05:35 PM
Pete Becker
Guest
 
Posts: n/a
Default 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)
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.