473,657 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Use of macros in C++?

A previous thread about macros just reminded me to ask this. I have
avoided using macros and defined some constants i.e.:

const int BYTES_PER_TRACK = 10 * 256;
const int TRACK0_SS_OFFSE T = 0;
const int TRACK0_SS_OFFSE T = 10 * 256;

Now, the problem is that I have several modules in my project that
references these constants, so I have to use 'extern const in
BYTES_PER_TRACK ' etc., in the main header file so the constants are
available to other modules.

I feel that in a situation like that wouldn't it be better to use
macros intead? Or not?

Thanks for any insights offered.
--
http://www.munted.org.uk

Take a nap, it saves lives.
May 7 '06 #1
17 1697
* Alex Buell:
A previous thread about macros just reminded me to ask this. I have
avoided using macros and defined some constants i.e.:

const int BYTES_PER_TRACK = 10 * 256;
const int TRACK0_SS_OFFSE T = 0;
const int TRACK0_SS_OFFSE T = 10 * 256;
Use of all uppercase for names is likely to conflict with macros; it's a
bad idea (see this group's FAQ, and also Bjarne Stroustrup's FAQ).

It's a Java convention, where it isn't a problem because Java doesn't
have a preprocessor.

Java got it from early C (which didn't have 'const', so that the
preprocessor had to be used for constants, where that convention helped
to avoid conflict with ordinary non-preprocessor symbols).

Essentially you've taken something very bad from the mists of historical
time, and reintroduced it in a descendant language of the language where
it originated -- a descendant language that helped to expel the monster.

Don't copy conventions and other things blindly.

Now, the problem is that I have several modules in my project that
references these constants, so I have to use 'extern const in
BYTES_PER_TRACK ' etc., in the main header file
Well, other don't have to do that.

so the constants are
available to other modules.
They should be defined or at least declared in a header file, yes.

I feel that in a situation like that wouldn't it be better to use
macros intead? Or not?

Thanks for any insights offered.


Follow the net directions indicated above.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
May 7 '06 #2
On Sun, 7 May 2006 10:35:45 +0100, Alex Buell
<al********@mun ted.org.uk> wrote:
A previous thread about macros just reminded me to ask this. I have
avoided using macros and defined some constants i.e.:

const int BYTES_PER_TRACK = 10 * 256;
const int TRACK0_SS_OFFSE T = 0;
const int TRACK0_SS_OFFSE T = 10 * 256;

Now, the problem is that I have several modules in my project that
references these constants, so I have to use 'extern const in
BYTES_PER_TRAC K' etc., in the main header file so the constants are
available to other modules.

I feel that in a situation like that wouldn't it be better to use
macros intead? Or not?


Consider using enums:

enum {
BYTES_PER_TRACK = 10 * 256;
TRACK0_SS_OFFSE T = 0;
// ...
};

Best wishes,
Roland Pibinger
May 7 '06 #3
On Sun, 7 May 2006 10:35:45 +0100 Alex Buell <al********@mun ted.org.uk>
waved a wand and this message magically appeared:
const int TRACK0_SS_OFFSE T = 0;
const int TRACK0_SS_OFFSE T = 10 * 256;


Err, the second one should be:

const int TRACK0_DS_OFFSE T = 10 * 256;

--
http://www.munted.org.uk

Take a nap, it saves lives.
May 7 '06 #4
"Alex Buell" <al********@mun ted.org.uk> wrote in message
news:2006050710 3545.8dea4b40.a l********@munte d.org.uk
A previous thread about macros just reminded me to ask this. I have
avoided using macros and defined some constants i.e.:

const int BYTES_PER_TRACK = 10 * 256;
const int TRACK0_SS_OFFSE T = 0;
const int TRACK0_SS_OFFSE T = 10 * 256;

Now, the problem is that I have several modules in my project that
references these constants, so I have to use 'extern const in
BYTES_PER_TRACK ' etc., in the main header file so the constants are
available to other modules.

I feel that in a situation like that wouldn't it be better to use
macros intead? Or not?

Macros are only visible within the translation unit in which they are
#defined, which is the same as const ints (without the extern modifier). So
what advantage do macros offer?

--
John Carson
May 7 '06 #5
On Sun, 07 May 2006 09:58:58 GMT rp*****@yahoo.c om (Roland Pibinger)
waved a wand and this message magically appeared:
I feel that in a situation like that wouldn't it be better to use
macros intead? Or not?


Consider using enums:

enum {
BYTES_PER_TRACK = 10 * 256;
TRACK0_SS_OFFSE T = 0;
// ...
};


That along with putting it into its own name space might be a better
idea. Thanks.

--
http://www.munted.org.uk

Take a nap, it saves lives.
May 7 '06 #6
Alex Buell wrote:
A previous thread about macros just reminded me to ask this. I have
avoided using macros and defined some constants i.e.:

const int BYTES_PER_TRACK = 10 * 256;
const int TRACK0_SS_OFFSE T = 0;
const int TRACK0_SS_OFFSE T = 10 * 256;

Now, the problem is that I have several modules in my project that
references these constants, so I have to use 'extern const in
BYTES_PER_TRACK ' etc., in the main header file so the constants are
available to other modules.

I feel that in a situation like that wouldn't it be better to use
macros intead? Or not?


Not. Just define the constant in the header and don't make it extern.
May 7 '06 #7
On Sun, 07 May 2006 12:47:53 +0200 Rolf Magnus <ra******@t-online.de>
waved a wand and this message magically appeared:
I feel that in a situation like that wouldn't it be better to use
macros intead? Or not?


Not. Just define the constant in the header and don't make it extern.


My preferred solution is:
namespace DISK
{
enum
{
BYTES_PER_TRACK = 10 * 256,

TRACK0_SS_OFFSE T = 0,
TRACK0_DS_OFFSE T = 10 * 256
};
};

And refer to them as:

if (ds)
offset = DISK::TRACK0_DS _OFFSET;

It's much nicer.
--
http://www.munted.org.uk

Take a nap, it saves lives.
May 7 '06 #8
Alex Buell wrote:
On Sun, 07 May 2006 12:47:53 +0200 Rolf Magnus <ra******@t-online.de>
waved a wand and this message magically appeared:
> I feel that in a situation like that wouldn't it be better to use
> macros intead? Or not?


Not. Just define the constant in the header and don't make it extern.


My preferred solution is:
namespace DISK
{
enum
{
BYTES_PER_TRACK = 10 * 256,

TRACK0_SS_OFFSE T = 0,
TRACK0_DS_OFFSE T = 10 * 256
};
};

And refer to them as:

if (ds)
offset = DISK::TRACK0_DS _OFFSET;

It's much nicer.


Not really, IMHO. enum is for enumerated types, not for constants.

May 7 '06 #9
On Sun, 07 May 2006 13:43:02 +0200, Rolf Magnus <ra******@t-online.de>
wrote:
Alex Buell wrote:
My preferred solution is:
namespace DISK
{
enum
{
BYTES_PER_TRACK = 10 * 256,

TRACK0_SS_OFFSE T = 0,
TRACK0_DS_OFFSE T = 10 * 256
};
};

And refer to them as:

if (ds)
offset = DISK::TRACK0_DS _OFFSET;

It's much nicer.


Not really, IMHO. enum is for enumerated types, not for constants.


Note that an anonymous ("typeless") enum is used. :-)
May 7 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
2980
by: Chris Reedy | last post by:
For everyone - Apologies for the length of this message. If you don't want to look at the long example, you can skip to the end of the message. And for the Python gurus among you, if you can spare the time, I would appreciate any comments (including words like evil and disgusting, if you think they are applicable :-}) on the example here. Kenny -
699
33864
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
16
2410
by: mike420 | last post by:
Tayss wrote: > > app = wxPySimpleApp() > frame = MainWindow(None, -1, "A window") > frame.Show(True) > app.MainLoop() > Why do you need a macro for that? Why don't you just write
37
2793
by: michele.simionato | last post by:
Paul Rubin wrote: > How about macros? Some pretty horrible things have been done in C > programs with the C preprocessor. But there's a movememnt afloat to > add hygienic macros to Python. Got any thoughts about that? "Movement" seems quite an exaggeration. Maybe 2-3 people made some experiments, but nobody within the core Python developers seems to be willing to advocate the introduction of macros. > Why should you care whether the...
8
7224
by: Michael Winter | last post by:
In a recent post ("About C error" by Victor, 21 Sep 2003), comments were made about the poster's use of macros. What I would like to know is why they are considered bad? I'm not referring to their use as 'functions'; I realise the loss of type-safety and the possible evaluation errors that can occur. However, what would be the harm with numeric and text literals? Consider a number that plays a significant role in the implementation of...
11
2843
by: Ben Hetland | last post by:
....in certain cituations they can be useful if not for anything else, then at least for saving a lot of repetetetetetitititive typing. :-) Beyond the point of "do something better instead", I'm curious about how the following syntactical problem can be solved. It should apply equally to C and C++ as it mainly is a preprocessor-related problem. I tryed to define something similar to the following example: ...
3
2661
by: Stephen Sprunk | last post by:
On a project I'm working on, I ran across the following macros: /* assume s is struct stream *, s->p is char, v is unit16_t or uint32_t */ #define in_uint16_le(s,v) { v = *((s)->p++); v += *((s)->p++) << 8; } #define in_uint32_le(s,v) { in_uint16_le(s,v) \ v += *((s)->p++) << 16; v += *((s)->p++) << 24; } I'm personally not fond of function-like macros and wanted to turn these into static inline functions, but I'm having trouble doing...
47
32901
by: Emil | last post by:
Is there any hope that new versions of PHP will support macros similar to C or C++? I've searched manual and didn't find anything except define directive, but it can be used to define constant values only. Of course it is not THAT neccessary functionality, but it could be very useful. greetz Emil
11
22349
by: San | last post by:
hi there, I am new to c++ and tryig to learn the basics of the c++ concepts. While I was reading the templates, I realize that the templates are a syntax that the compilar expands pased upon the type specified. This is much similar like a macro expansion in C. can anyone please explain advantages of one over the other ? Thanks in advance -
33
8875
by: Robert Seacord | last post by:
When writing C99 code is a reasonable recommendation to use inline functions instead of macros? What sort of things is it still reasonable to do using macros? For example, is it reasonable to write type generic functions macros? #define CUBE(I) ( (I) * (I) * (I) ) Is there some more concise set of things where inline functions should be used instead of macros? Multi-statement macros, for example?
0
8403
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8737
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7345
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6174
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2735
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1730
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.