Click here to Skip to main content
15,399,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am "rewriting" a piece of C code to C++ class which has few hardware #defines.

They are related to the class - so where do these #defines belong - header / source / elsewhere ?

What I have tried:

For now I am leaving it alone - sort of "global "... not very clean...
Posted
Updated 19-Sep-21 20:25pm

My preference is that if the type is known, use a constexpr with as narrow a scope as possible. In the rare circumstance that the value covers multiple types, I use a define.
   
I agree with Joe Woodbury on this. I will add that I would try to use a class with accessor methods and isolate as much knowledge of the specifics of that hardware as possible into that class. Usually that means only the source code for the class needs to know the values you are defining.

I also try to avoid behavioral definitions and rely on logic to handle variations in behavior. I really, REALLY dislike ifdefs because usually not all code paths get compiled all the time.
   
Comments
Member 14968771 20-Sep-21 11:47am
   
The original code was written for at least two different hardware...
I am really not interested to write for something I will not use , but these specific #define(s) are already (scattered) in the code. I guess I'll put them in class header.
Rick York 20-Sep-21 18:16pm
   
If at all possible, I would try to avoid that because it exposes higher level code to details it probably does not need to know.

FWIW, I am in the automation business and I have been making interfaces to hardware for decades and I have found it enhances the usability and reusability of an interface when as few details as possible are known.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900