Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

(Simple) #define v.s. const float

This topic is 6101 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was reading somewhere that its much better to have const floats than #defines in your code for debug purposes. fine .. but I have 3 files CTriangle, CSquare, and CHex. All of them are going to use const float PIover180 = 0.0174532925f; If I put this global there will be problems with redefination etc , If I put this as a private member you cant initalize a const float int the constructor , as Its const (duh) , and you cant initalise it in the header. what Im using at the moment is #ifndef PIover180 #define PIover180 0.0174532925f #endif But its a bit crude. How do I clean this up ? thanks

Share this post


Link to post
Share on other sites
Advertisement
To initialize a const data member you have to use the constructor initializer list. This is the only place where you can assign a value to a const variable.

  
class Bleh
{
private:
const float blah;
const int theAnswer;
public:
Bleh() : blah( 0.0174532925f ), theAnswer( 42 )
{};
};

Share this post


Link to post
Share on other sites
quote:
Original post by SCRUB
I was reading somewhere that its much better to have const floats than #defines in your code for debug purposes.


For debug purposes, name pollution (#defines don't obey scope), type safety, #defines may overwrite statements unintentionally, complex constants may have problems with parenthesis, etc.

quote:

fine .. but I have 3 files CTriangle, CSquare, and CHex.
All of them are going to use

const float PIover180 = 0.0174532925f;

If I put this global there will be problems with redefination etc


Really? Have you tried it? Because it works fine on my machine.
quote:

If I put this as a private member you cant initalize a const float int the constructor , as Its const (duh)


Arild Fines showed how to do this with ctor initialization lists. You can (should) make the constant a static member if you make it part of a struct or class:
    
// globals.h

struct Globals
{
static const float PIover180;
};
// globals.cpp

const float Globals::PIover180 = 0.0174532925f;


However, this has the same problem as just making it a normal class member in that the value of the variable is not known at compile time, and therefore cannot be optimized out; in this way, it does not behave like a #define.

quote:

and you cant initalise it in the header.



Not unless you inline the ctor.

quote:

what Im using at the moment is

#ifndef PIover180
#define PIover180 0.0174532925f
#endif

But its a bit crude.

How do I clean this up ?



  
// ProjNameTrigGlobals.h

namespace ProjNameTrigGlobals
{
const float PIover180 = 0.0174532925f;
}


Edited by - Stoffel on September 7, 2001 10:29:04 AM

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!