🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

5 total messages Started by Brian V. McGroarty Sat, 30 Mar 1996 05:46
Const class member
#3867
Author: Brian V. McGroarty
Date: Sat, 30 Mar 1996 05:46
30 lines
1967 bytes
Is this legal?  Borland and Microsoft compilers will accept the following:

class AnyClass
{
    const int constInt;
}

The Borland compiler complains about the uninitialized constant, whereas
the Microsoft compiler does not.  If a constructor is present, both will
complain that the constant isn't initialized in the constructor, however
neither will allow you to assign a value in the constructor by simply
specifying "constInt= some value".  I have also attempted to initialize in
a global variable "int AnyClass::constInt",  to determine whether static
somehow became implicit -- still no go.  If this is legal, how is the value
initialized?

Would compilers stand a better chance of optimizing given the above via
constant propogation through inlined (member only?) functions applied to a
hard-coded instance of the object, or would it just be to help prevent
programmers' error?

---
Brian Valters McGroarty -- brianmcg@bix.com
phone/fax (847) 439-7714
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
Re: Const class member
#3874
Author: Simon Crase
Date: Sun, 31 Mar 1996 02:20
51 lines
2815 bytes
Brian V. McGroarty (brianmcg@interaccess.com) wrote:
: Is this legal?  Borland and Microsoft compilers will accept the following:

: class AnyClass
: {
:     const int constInt;
: }

: The Borland compiler complains about the uninitialized constant, whereas
: the Microsoft compiler does not.  If a constructor is present, both will
: complain that the constant isn't initialized in the constructor, however
: neither will allow you to assign a value in the constructor by simply
: specifying "constInt= some value".  I have also attempted to initialize in
Have you tried:
AnyClass::AnyClass
	: constInt(42)
{
}
The geenral idea seems to be to disallow any piece of code that assigns
a value using constInt=....
: a global variable "int AnyClass::constInt",  to determine whether static
: somehow became implicit -- still no go.  If this is legal, how is the value
: initialized?

: Would compilers stand a better chance of optimizing given the above via
: constant propogation through inlined (member only?) functions applied to a
: hard-coded instance of the object, or would it just be to help prevent
: programmers' error?

: ---
: Brian Valters McGroarty -- brianmcg@bix.com
: phone/fax (847) 439-7714
: ---
: [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
: [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
: [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
: [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
: [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]

--
Simon A. Crase 		Emerson & Crase Consultants - ++61-3-9592-1764
Artificial Intelligence, Graphical User Interfaces, C++,
Object Oriented Design, Microsoft Windows Applications
s.crase@ieee.org = tangaroa@werple.net.au


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
Re: Const class member
#3887
Author: Torsten Hoff
Date: Tue, 02 Apr 1996 13:30
76 lines
3471 bytes
In article <4jgpqa$t09@nntp.interaccess.com>,
   brianmcg@interaccess.com (Brian V. McGroarty) wrote:
>Is this legal?  Borland and Microsoft compilers will accept the following:
>
>class AnyClass
>{
>    const int constInt;
>}
>
>The Borland compiler complains about the uninitialized constant, whereas
>the Microsoft compiler does not.  If a constructor is present, both will
>complain that the constant isn't initialized in the constructor, however
>neither will allow you to assign a value in the constructor by simply
>specifying "constInt= some value".  I have also attempted to initialize in
>a global variable "int AnyClass::constInt",  to determine whether static
>somehow became implicit -- still no go.  If this is legal, how is the value
>initialized?
[Snip]

That's perfectly legal.

However, you can't initialize the const member in the class
declaration, since you haven't instantiated an object of the class in
question which could receive the value. Furthermore, it would prevent
you from assigning different values to constInt in different instances
of the class, which in most cases is not what you want, either.

If you *really* want compile-time initialized constants, and can live
with something that has essentially the same properties as an integer,
use class-scope enums. If you need several compile-time constants with
the same value, you can use multiple untagged enums:

class AnyClass
{
    enum {FOO = 1, BAR = 1}; // multiple enums with same value; doesn't work!
    enum {FOO = 1};          // OK
    enum {BAR = 1};          // OK
}

To continue with the const class member for a moment, you can't
initialize the const class member in your constructor, as you already
noted -- this must be done using the initialization list. You can do
this as follows:

AnyClass::AnyClass() : constInt(0)
    {
    // other initialization code goes here
    }

or

AnyClass::AnyClass(int ConstValue) : constInt(ConstValue)
    {
    // other initialization code goes here
    }

The initialization is actually performed before your constructor is
entered; if more than one member is being initialized, it happens in
the order in which the members are declared in the class, *not* the
order in which they are listed in the initialization list.

I hope this helps!


Torsten Hoff
thoff@symantec.com

(The views and opinions expressed are my own, and
should not be construed as representing those of
Symantec Corporation)
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
Re: Const class member
#3909
Author: James Kanze US/ESC 60/3/141 #407...
Date: Wed, 03 Apr 1996 17:22
74 lines
3866 bytes
In article <4jq60l$6pp@symiserver2.symantec.com> thoff@symantec.com
(Torsten Hoff) writes:

|> In article <4jgpqa$t09@nntp.interaccess.com>,
|>    brianmcg@interaccess.com (Brian V. McGroarty) wrote:
|> >Is this legal?  Borland and Microsoft compilers will accept the following:
|> >
|> >class AnyClass
|> >{
|> >    const int constInt;
|> >}
|> >
|> >The Borland compiler complains about the uninitialized constant, whereas
|> >the Microsoft compiler does not.  If a constructor is present, both will
|> >complain that the constant isn't initialized in the constructor, however
|> >neither will allow you to assign a value in the constructor by simply
|> >specifying "constInt= some value".  I have also attempted to initialize in
|> >a global variable "int AnyClass::constInt",  to determine whether static
|> >somehow became implicit -- still no go.  If this is legal, how is the value
|> >initialized?
|> [Snip]

|> That's perfectly legal.

|> However, you can't initialize the const member in the class
|> declaration, since you haven't instantiated an object of the class in
|> question which could receive the value. Furthermore, it would prevent
|> you from assigning different values to constInt in different instances
|> of the class, which in most cases is not what you want, either.

|> If you *really* want compile-time initialized constants, and can live
|> with something that has essentially the same properties as an integer,
|> use class-scope enums. If you need several compile-time constants with
|> the same value, you can use multiple untagged enums:

|> class AnyClass
|> {
|>     enum {FOO = 1, BAR = 1}; // multiple enums with same value; doesn't work!

Why not?  It is allowed by the standard, and works on all of the
compilers I've used.

|>     enum {FOO = 1};          // OK
|>     enum {BAR = 1};          // OK
|> }

The (minor) problem with this solution is that the type of the constant
may not be what you want.  This is probably only a problem when function
overloading is present, or templates.

The draft standard allows an initialization of a *static* const in the
class definition, e.g.:

    class X
    {
        static size_t const     mySize = 1024 ;
    } ;

This is a relatively new feature, however, and may not be supported by
your compiler.

	[Rest of correct answer deleted...]
-- 
James Kanze         Tel.: (+33) 88 14 49 00        email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils, études et réalisations en logiciel orienté objet --
                -- A la recherche d'une activité dans une region francophone
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu 
]
Re: Const class member
#3910
Author: David Vandevoorde
Date: Wed, 03 Apr 1996 17:22
38 lines
2003 bytes
>>>>> "TH" == Torsten Hoff <thoff@symantec.com> writes:
[...]
TH> If you *really* want compile-time initialized constants, and can live
TH> with something that has essentially the same properties as an integer,
TH> use class-scope enums. If you need several compile-time constants with
TH> the same value, you can use multiple untagged enums:

TH> class AnyClass
TH> {
TH>     enum {FOO = 1, BAR = 1}; // multiple enums with same value; 
                                 //    doesn't work!
TH>     enum {FOO = 1};          // OK
TH>     enum {BAR = 1};          // OK
TH> }
[...]

Alternatively:

	struct AnyStruct {
	    static bool const Yes = true;
	    static char const A = 'B';
	};

(Somewhere:
	bool const AnyStruct::Yes;
	char const AnyStruct::A;
)

which works for integral types (if you compiler is reasonably up-to-date).

	Daveed
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu 
]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads