-
May 9th, 2020, 01:17 AM
#1
enum or static const
I have some legacy code, where they use the magic numbers to index arrays.
Snippet is :
Code:
if(pLossMultiRaster || pSBATrafficLossMultiRaster)
{
// NOTE - SPECIFIC ORDER IS IMPORTANT!
INT_PTR *SBAarray[12];
SBAarray[0] = reinterpret_cast<INT_PTR*>(pSBAControlLossMultiRaster);
SBAarray[1] = reinterpret_cast<INT_PTR*>(pSBABestControlBeamIndexMultiRaster);
Im thinking to replace it with enum, Or static const int .. Any comments on Which one is better to use the enum or static const ?
Thanks
pdk
Last edited by 2kaud; May 9th, 2020 at 02:29 AM.
Reason: Added code tags
-
May 9th, 2020, 01:52 AM
#2
Re: enum or static const
Victor Nijegorodov
-
May 9th, 2020, 02:31 AM
#3
Re: enum or static const
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 9th, 2020, 04:05 PM
#4
Re: enum or static const
Thanks a lot for the replies Victor and kaud
-
May 10th, 2020, 02:59 AM
#5
Re: enum or static const
Originally Posted by pdk5
Which one is better to use the enum or static const ?
Maybe neither is
If the sole purpose of the "magic numbers" is to associate each multiraster with a number and then use the numbers as alias names for the multirasters then you can as well define symbolic names for the multirasters, something like
Code:
INT_PTR* const sSBAControlLossMR = reinterpret_cast<INT_PTR*>(pSBAControlLossMultiRaster);
INT_PTR* const sSBABestControlBeamIndexMR = reinterpret_cast<INT_PTR*>(pSBABestControlBeamIndexMultiRaster);
These can be used directly (instead of indirectly by way of the associated numbers) and you have removed one, presumably unnecessary, level of indirection.
But if the multiraster-number association serves a wider purpose and must remain then I also find an enum to be the best replacment for the "magic numbers".
Last edited by wolle; May 10th, 2020 at 11:59 PM.
-
May 11th, 2020, 01:05 AM
#6
Re: enum or static const
> Im thinking to replace it with enum, Or static const int
Well if you're going to be editing each index, why not just go straight to a struct, which gives you automatic names to start with.
Code:
// NOTE - SPECIFIC ORDER IS IMPORTANT!
struct foo {
INT_PTR *ControlLossMultiRaster;
INT_PTR *BestControlBeamIndexMultiRaster;
// 10 more
} SBAthingy;
// INT_PTR *SBAarray[12];
SBAthingy.ControlLossMultiRaster = reinterpret_cast<INT_PTR*>(pSBAControlLossMultiRaster);
SBAthingy.BestControlBeamIndexMultiRaster = reinterpret_cast<INT_PTR*>(pSBABestControlBeamIndexMultiRaster);
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|