Search code examples
c++backwards-compatibility

What is the difference, usage-wise, between defines/macros/structs and consts/funcs/classes? (C++)


I know that the difference between defines and constants is that constants have type, and that between macros and functions, functions are called, and typed, whereas macros are untyped inline. Not so much the difference between structs and classes, but I don't think there is one, besides the public/private default thing (and I'm taking about only C++ here, not C).

Anyways, are there any times that it would be more efficient, beneficial, or, at the very least, cleaner code to use defines/macros/structs than it would be to use the C++ versions, constants, functions, and classes?

Specifically, and because this is only one without a clear difference (at least in the final program) between them, is it ever better to use structs than classes, and if there isn't efficiency-wise, what conventions for using them do you use?


Solution

  • Typically, in a C++ program I might use a struct for a simple aggregation of data, such as a Point structure that contains an x and a y. I would use a class for objects that have behaviour (member functions) associated with them.

    This is just a convention of course, since the compiler treats them almost identically except for the two details mentioned above (default inheritance and default member visibility).