Re: Constant evaluation

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



ZartPARZ wrote:
Could you told me adventage/disadventage of #define and const? (for
numerical constant) Which one I should use?

Macros are evil, see the C++ FAQ at parashift.com.

I have one more question, too, why did you quote this:

"Carl Daniel [VC++ MVP]" wrote:

ZartPARZ wrote:
Hi

If I have a code like

#define mass 2.4
#define g 9.8

double potential(double height)
{
return mass * g * height;
}

My question is the mass*g part will be calculate at compile time or
run-time? And if I use const double mass=2.4 instead, when is it
calculated?

In an optimized (Release) build, the compiler will perform the
calculation of mass*g at compile time regardless of whether you use a
#define or a const double.

-cd




?

Don't top-post, don't full-quote unnecessarily.

Uli

.



Relevant Pages

  • Re: Constant evaluation
    ... return mass * g * height; ... And if I use const double mass=2.4 instead, ... the compiler will perform the calculation ... of mass*g at compile time regardless of whether you use a #define or a const ...
    (microsoft.public.vc.language)
  • Re: Does C# have static local variables like C++?
    ... the compile time errors ... my complicated hierarchy was wrong. ... const does. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Does C# have static local variables like C++?
    ... void sqrval(const int &val) ... I think people think that because const ... True, but as I said it's too late, I tried very hard to write const correct code using C++/CLI and finaly gave up because all I had to do was const_cast object references passed as parameters to the FCL methods to finally fnd out that a const_cast is not permitted on all reference types. ... The point is the benefit it brings at compile time. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: [PHP] class const versus define
    ... Richard Lynch schreef: ... const is compile time, define is runtime ... IIRC const was made this way in order to make it fast. ...
    (php.general)
  • Re: Does C# have static local variables like C++?
    ... void sqrval(const int &val) ... I think people think that because const ... The point is the benefit it brings at compile time. ... designers found they couldn't enforce const vars to be unchanging that ...
    (microsoft.public.dotnet.languages.csharp)