-2
\$\begingroup\$

Recently I have been using more macros in my C++ code. I wrote a class with a private static const float because it is useful to prevent making mistakes. Then I thought, why use a private static const if a macro could solve the same problem, without taking up 4 bytes of memory?

#pragma once

#include "VehicleType.h"

#define TOTAL_TIME 5.0f;

class Spawn
{
    VehicleType vehicleType;
    float wait = TOTAL_TIME;
public:
    Spawn(VehicleType vehicleType) :
        vehicleType(vehicleType)
    {}

    void Update(const float deltaTime);
    bool IsReady() const { return wait <= 0; };
    void Timeout() { wait = TOTAL_TIME; }; 

    VehicleType GetVehicleType() { return vehicleType; }
};

#undef TOTAL_TIME

Is this a good solution or should I stick with the static const way? Aside from the memory usage, it also takes up two lines in the code, just like the macro (#define and #undef).

#pragma once

#include "VehicleType.h"

class Spawn
{
    static const float TOTAL_TIME;

    VehicleType vehicleType;
    float wait = TOTAL_TIME;
public:
    Spawn(VehicleType vehicleType) :
        vehicleType(vehicleType)
    {}

    void Update(const float deltaTime);
    bool IsReady() const { return wait <= 0; };
    void Timeout() { wait = TOTAL_TIME; }; 

    VehicleType GetVehicleType() { return vehicleType; }
};

const float Spawn::TOTAL_TIME = 5.0f;
\$\endgroup\$

closed as unclear what you're asking by 200_success Dec 19 '16 at 21:44

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    \$\begingroup\$ Your first assumption is that it takes up space. Modern compilers will probably use zero space (as long as you don't take it's address) if the value is available from the header file. BUT even if it is not 4 bytes on a modern machine is nothing (even on mobile devices nowadays). You have to be on a very small micro controller before I would care about 4 bytes. \$\endgroup\$ – Martin York Dec 19 '16 at 21:00
  • 1
    \$\begingroup\$ What task does this code accomplish? Please explain. Also, why is this question tagged with c, when the code will clearly now compile as C? \$\endgroup\$ – 200_success Dec 19 '16 at 21:44
  • \$\begingroup\$ @200_success The constant in the code makes sure I won't have to edit every place I use the TOTAL_TIME, in case I want to change the value. Macros are from C. \$\endgroup\$ – oddRaven Dec 19 '16 at 22:08
  • 1
    \$\begingroup\$ Most of C++ is derived from C, but C++ is still C++. \$\endgroup\$ – 200_success Dec 19 '16 at 22:17
  • 1
    \$\begingroup\$ Please explain the intended purpose of this code, though, as per the How to Ask guidelines. \$\endgroup\$ – 200_success Dec 19 '16 at 22:18
1
\$\begingroup\$

Yes it is a bad idea.

TOTAL_TIME being a macro pollutes the whole namespace with its mess.

In C++11 the ability to initialize these values in the class definition was introduced. Use that instead.

class Spawn
{
    static constexpr float totalTime = 5.0f;
};
\$\endgroup\$
  • \$\begingroup\$ Not that it matters much, but does the 4 bytes of memory gets freed up after the class has been initialized? \$\endgroup\$ – oddRaven Dec 19 '16 at 21:12
  • \$\begingroup\$ @oddRaven: Why would there be 4 (or sizeof(float)) bytes of memory used? \$\endgroup\$ – Martin York Dec 19 '16 at 21:14
  • \$\begingroup\$ A float is 4 bytes in size. Does it have any impact on the memory compared to a macro? \$\endgroup\$ – oddRaven Dec 19 '16 at 21:15
  • \$\begingroup\$ @oddRaven: A float may be 4 bytes on your current configuration (Hardware/OS/Compiler). But the language does not guarantee that. In C++ there is no need for macros (for programming). For every use case of a C macro the C++ language provides a language alternative (apart from maybe conditional compilation). \$\endgroup\$ – Martin York Dec 20 '16 at 1:40
0
\$\begingroup\$
  1. In C, the only way to declare compile time constants is not by using const, but rather with the use of macros (#define).

    C++ makes use of const as well as constexpr (c++11) to declare constants, thus discarding the need for #define.

  2. Macros are public to whomever "includes" the file they are declared in, but private members of a class are only visible to the object of that class.

    If you want your constant to be public or private, declare it in your class as such and initialise it. You don't need a macro for that.

\$\endgroup\$
  • \$\begingroup\$ On your second point, doesn't undef in the question fully cover the visibility towards the other classes? \$\endgroup\$ – oddRaven Dec 19 '16 at 21:38
  • \$\begingroup\$ @oddRaven, I guess it does, but I'd argue that it's quite an ugly way of doing that whereas a constant declared within the class will make it obvious that TOTAL_TIME is a private constant \$\endgroup\$ – smac89 Dec 19 '16 at 21:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.