CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: enum or static const

  1. #1
    Join Date
    May 2015
    Posts
    277

    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

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,759

    Re: enum or static const

    I'd implement the enum.
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,193

    Re: enum or static const

    I'd use enum also.
    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++17 Compiler: Microsoft VS2019 (16.7.0)

  4. #4
    Join Date
    May 2015
    Posts
    277

    Re: enum or static const

    Thanks a lot for the replies Victor and kaud

  5. #5
    Join Date
    Feb 2017
    Posts
    550

    Re: enum or static const

    Quote Originally Posted by pdk5 View Post
    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.

  6. #6
    Join Date
    Nov 2018
    Posts
    36

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)