Topic: #define vs const
Author: shepherd@debussy.sbi.com (Marc Shepherd)
Date: Thu, 21 Apr 1994 11:42:18 GMT Raw View
In article Iuy@netcom.com, rfg@netcom.com (Ronald F. Guilmette) writes:
> In article <2odg5l$36b@mizar.usc.edu> spitzak@mizar.usc.edu (William Spitzak) writes:
> >Could somebody explain why these won't work (is it my compiler (sgi)
> >or is it something in the standard):
> >
> > class a {
> > const int sizeofarray = 42;
> > int array[sizeofarray];
> > public:
> > const float usefulnumber = 1.32938;
> > };
Semantically, what the writer is proposing makes a lot of sense, but it doesn't
happen to be legal C++. However, the same effect can be achieved easily enough:
class a {
{
enum { sizeofarray = 42 };
int array[sizeofarray];
a() { usefulnumber = 1.32937F; }
public:
const float usefulnumber;
};
The only two changes are that:
-- sizeofarray is an enumerator, instead of a plain const int
-- usefulnumber is initialized in the constructor
>
> A somewhat more interesting set of questions is raised by the following
> slightly different example:
>
> class a {
> public:
> static const int sizeofarray;
> static int array[];
> };
>
> int a::sizeofarray = 42;
> int a::array[a::sizeofarray]; // ??
>
> At this point in time, I doubt that any two compilers will agree upon what
> is right or what is wrong with the code above. I further doubt that the
> current draft standard offers any unambiguous guidance for determining
> (independent of implementations) whether the above code in invalid or not
> (or why).
>
cfront compiles the above code without complaint.
---
Marc Shepherd
Salomon Brothers Inc
mshepherd@mhfl The opinions I express are no one's but mine!
Author: simon@sco.COM (Simon Tooke)
Date: Thu, 21 Apr 1994 12:46:15 GMT Raw View
In <rfgCoG9su.Iuy@netcom.com> rfg@netcom.com (Ronald F. Guilmette) writes:
>A somewhat more interesting set of questions is raised by the following
>slightly different example:
> class a {
> public:
> static const int sizeofarray;
> static int array[];
> };
> int a::sizeofarray = 42;
> int a::array[a::sizeofarray]; // ??
>At this point in time, I doubt that any two compilers will agree upon what
>is right or what is wrong with the code above. I further doubt that the
>current draft standard offers any unambiguous guidance for determining
>(independent of implementations) whether the above code in invalid or not
>(or why).
I'd have to say the above code _would not_ compile, because the definition
of a::sizeofarray differs from the declaration inside class a - no const.
If a::sizeofarray was not const (in both places), I would bet a good compiler
woud reject the program. If a::sizeofarray was const, the same compiler
should accept the program.
-simon tooke
Author: rfg@netcom.com (Ronald F. Guilmette)
Date: Mon, 18 Apr 1994 10:14:54 GMT Raw View
In article <2odg5l$36b@mizar.usc.edu> spitzak@mizar.usc.edu (William Spitzak) writes:
>Could somebody explain why these won't work (is it my compiler (sgi)
>or is it something in the standard):
>
> class a {
> const int sizeofarray = 42;
> int array[sizeofarray];
> public:
> const float usefulnumber = 1.32938;
> };
A somewhat more interesting set of questions is raised by the following
slightly different example:
class a {
public:
static const int sizeofarray;
static int array[];
};
int a::sizeofarray = 42;
int a::array[a::sizeofarray]; // ??
At this point in time, I doubt that any two compilers will agree upon what
is right or what is wrong with the code above. I further doubt that the
current draft standard offers any unambiguous guidance for determining
(independent of implementations) whether the above code in invalid or not
(or why).
--
-- Ron Guilmette, Sunnyvale, CA ---------- RG Consulting -------------------
---- domain addr: rfg@netcom.com ----------- Purveyors of Compiler Test ----
---- uucp addr: ...!uunet!netcom!rfg ------- Suites and Bullet-Proof Shoes -
Author: rfg@netcom.com (Ronald F. Guilmette)
Date: Mon, 18 Apr 1994 10:30:52 GMT Raw View
In article <vantong.766327164@dutnak2> vantong@dutnak2.tn.tudelft.nl (Bart van Tongeren) writes:
>[ I redirected followups to comp.lang.c++ ]
>
>In <Co66z9.IsA@megatest.com> djones@megatest.com (Dave Jones) writes:
>
>]From article <2o4lkq$eh1@cronkite.ocis.temple.edu>, by wizard@astro.ocis.temple.edu (Bruce Weiner):
>]> I would like to know what I should use when writting a program.
>]> const float pi = 3.141;
>]> OR
>]> #define pi 3.141
>]Neither.
>]Try,
>] const float pi = 3.142;
>]or better yet,
>] const float pi = 3.1415926536;
>]I could go on and on.
>] Dave
>] djones@silly-math-triva
>Best:
> #include <math.h> /* defines M_PI */
Ah, excuse me, but just because the version of <math.h> your compiler
vendor gave you has some name space pollution in it (i.e. a #define of
a symbol M_PI) that doesn't mean that such a definition will appear in
any truly standard conforming <math.h> files.
--
-- Ron Guilmette, Sunnyvale, CA ---------- RG Consulting -------------------
---- domain addr: rfg@netcom.com ----------- Purveyors of Compiler Test ----
---- uucp addr: ...!uunet!netcom!rfg ------- Suites and Bullet-Proof Shoes -
Author: jvsb@sebb.bel.alcatel.be (Johan Vanslembrouck)
Date: 14 Apr 1994 06:51:11 GMT Raw View
In article <2odg5l$36b@mizar.usc.edu>, spitzak@mizar.usc.edu (William
Spitzak) writes:
|> Could somebody explain why these won't work (is it my compiler (sgi)
|> or is it something in the standard):
|>
|> class a {
|> const int sizeofarray = 42;
|> int array[sizeofarray];
|> public:
|> const float usefulnumber = 1.32938;
|> };
|>
|> void foo() {
|> float x = a::usefulnumber;
|> ...
|> }
|>
|> What I am trying to do is put accessable class-specific constants
|> in. This refuses to compile (with various errors).
|>
It's not your compiler. The ARM disallows initializers for data members
within a class declaration (section 9.4) and that's a pity.
An explanation is given at page 181, together with an alternative:
class a {
enum { sizeofarray = 42 }; // not that nice, isn't it ?
int array[sizeofarray];
...
I hope the ANSI committee (and the compiler constructors)
will spend some effort to make const int and (especially)
static const int initialization at class scope
possible anyway.
-----------------------------------------------------------------------
Johan Vanslembrouck - SE99 Tel : +32 3 2407739
Alcatel Bell Telephone Telex : 72128 Bella B
Francis Wellesplein 1 Fax : +32 3 2409932
B-2018 Antwerp e-mail : jvsb@sebb.bel.alcatel.be
Belgium
-----------------------------------------------------------------------
Author: vantong@dutnak2.tn.tudelft.nl (Bart van Tongeren)
Date: Thu, 14 Apr 1994 12:39:24 GMT Raw View
[ I redirected followups to comp.lang.c++ ]
In <Co66z9.IsA@megatest.com> djones@megatest.com (Dave Jones) writes:
]From article <2o4lkq$eh1@cronkite.ocis.temple.edu>, by wizard@astro.ocis.temple.edu (Bruce Weiner):
]> I would like to know what I should use when writting a program.
]> const float pi = 3.141;
]> OR
]> #define pi 3.141
]Neither.
]Try,
] const float pi = 3.142;
]or better yet,
] const float pi = 3.1415926536;
]I could go on and on.
] Dave
] djones@silly-math-triva
Best:
#include <math.h> /* defines M_PI */
const double pi = M_PI; // seems slightly redundant to me
Use const for your own constants, the generated symbol helps when
debugging and enables type checking by the compiler.
-= Bart =-
--
* Bart van Tongeren ----- Delft University of Technology, Netherlands *
| -------------------- Applied Physics Department, Spin Imaging Group |
| email tong@si.tn.tudelft.nl voice +31-15-784059 fax +31-15-624978 |
*---------------------------------------------------------------------*
Author: wizard@astro.ocis.temple.edu (Bruce Weiner)
Date: 8 Apr 1994 22:25:30 GMT Raw View
I would like to know what I should use when writting a program.
const float pi = 3.141;
OR
#define pi 3.141
Which would be the correct usage???
--
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Bruce Weiner wizard@astro.ocis.temple.edu%
%Temple University %
%Phila, pa. %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Author: jpcauvin@bongo.cc.utexas.edu (Roger L. Cauvin)
Date: 9 Apr 1994 17:37:47 GMT Raw View
In article <2o4lkq$eh1@cronkite.ocis.temple.edu>,
wizard@astro.ocis.temple.edu (Bruce Weiner) wrote:
> I would like to know what I should use when writting a program.
>
> const float pi = 3.141;
> OR
> #define pi 3.141
>
> Which would be the correct usage???
Both are correct in the sense of being legal and working reliably.
However, use of the const directive is generally the preferred style.
Roger
Author: clint@vsfl.demon.co.uk (clint)
Date: Mon, 11 Apr 1994 09:37:00 +0000 Raw View
In article <jpcauvin-090494123638@slip-1-27.ots.utexas.edu>, jpcauvin@bongo.cc.utexas.edu (Roger L. Cauvin) writes:
> In article <2o4lkq$eh1@cronkite.ocis.temple.edu>,
> wizard@astro.ocis.temple.edu (Bruce Weiner) wrote:
>
> > I would like to know what I should use when writting a program.
> >
> > const float pi = 3.141;
> > OR
> > #define pi 3.141
> >
> > Which would be the correct usage???
>
> Both are correct in the sense of being legal and working reliably.
> However, use of the const directive is generally the preferred style.
The important differences (my $0.02 says) are:
* with const, you get type checking; with #define, you don't.
* with const, you can view the value in the debugger, whereas with
#define, you can't (at least, in many debuggers).
These are particularly good reasons for using inline functions rather
than macro functions. I never use #define now, except for conditional
compilation.
Ian Cameron Smith | clint@vsfl.demon.co.uk | "Don't quote me"
Principal Software Engineer | Tel: +44 (0)425 474484 | -- me
Virtual Software Factory LTD | Fax: +44 (0)425 474233 |
------- xface (XPM3) available on request ------- PGP available -------
Author: spitzak@mizar.usc.edu (William Spitzak)
Date: 11 Apr 1994 23:47:17 -0700 Raw View
Could somebody explain why these won't work (is it my compiler (sgi)
or is it something in the standard):
class a {
const int sizeofarray = 42;
int array[sizeofarray];
public:
const float usefulnumber = 1.32938;
};
void foo() {
float x = a::usefulnumber;
...
}
What I am trying to do is put accessable class-specific constants
in. This refuses to compile (with various errors).
It is true that I can just declare "const int x" and add "const int
a::x = 10" below, but this does not work for the array size shown
above, and is just ugly and hard to read in the second case, and
may not result in very efficient implementation (because if the
actual declaration is in another file, I see no way the compiler
can optimize the constant value into the code!).
This is very annoying, and I have resorted to using fake one or two
entry enums for integer constants, and #define for the floats, to get
around this. Have I perhaps missed something obvious?
Bill
PS:
If that is solved, I would also really like to do something like
this. Anybody considering adding it:
class polygon {
public:
virtual const int sides = 0; // no way to do "pure virtual"
...
};
class square : polygon {
public:
const int sides = 4;
...
};
class triangle : polygon {
public:
const int sides = 3;
...
};
Obviously this could be done with a virtual function, but it also
seems obvious to me that the above could compile into much faster
code on any machine. This could be a big win if that number must
be referred to a lot. Compiler would also know that getting that
number has no side effects and the number will not change between
calls.
Thanks!
--
_ ______ _______ ______________________________
I_)o|| [_ ._ o|_ _ _.| spitzak@mizar.usc.edu
I_)||| __]|_)||_ /_(_||< spitzak@sunova.d2.com
----------|------------------------------------------
Author: yury@if.cit.nstu.nsk.su (Yury Kuznetsov)
Date: 12 Apr 1994 13:46:53 GMT Raw View
Bruce Weiner (wizard@astro.ocis.temple.edu) wrote:
: I would like to know what I should use when writting a program.
: const float pi = 3.141;
: OR
: #define pi 3.141
: Which would be the correct usage???
In ARM and my opinion better using const float:
const float pi - ordinal local symbol in .text segment; has float type -
but #define pi - has double (!) type.
Author: djones@megatest.com (Dave Jones)
Date: Tue, 12 Apr 1994 23:28:16 GMT Raw View