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

Thread: enum versus private enum

  1. #1
    Join Date
    Mar 2013
    Posts
    5

    enum versus private enum

    The following code works in VC++ 2010 but generates numerous errors in VC++ 2012:

    #include "stdafx.h"

    using namespace System;

    enum class Months{January = 1, February, March, April };

    int main(array<System::String ^> ^args)
    {
    Months month;
    int value;

    month = Months::January;
    value = safe_cast<int>(month);
    Console::WriteLine(L"Month is {0} and the value is {1}.", month, value);



    return 0;
    }

    In order to compile in VC++ 2012, the code must be changed to:

    private enum class Months{January = 1, February, March, April };

    My question is why?
    Is VC++ so chaotic that code from one version won't compile in another version? Doesn't that defy the idea of portable code?

    When does one use enum and when does one use private enum?

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,591

    Re: enum versus private enum

    I wouldn't expect your safe_cast to work in any version of C++/CLI or the .NET framework. This cast is meant to be used for downcasting in a descendant/ancestor relationship, i.e. casting an A ^ to a B ^ where the actual object the A ^ refers to is of type B which is derived from A, or a type derived from B. There's no such relationship between System::Int32 (for which int is a synonyme) and a managed enum. They both derive from System::ValueType, directly or indirectly, but that rather gives them some sort of uncle/nephew relationship.

    There is an existing conversion between the two, however, so I think a static_cast would probably do the trick, at least for the conversion from enum to int, though I'm not actually 100% sure off the top of my head.

    And I don't think the assembly-level visibility (i.e. public vs. private enum class) has anything to do with that at all.
    Last edited by Eri523; March 13th, 2013 at 09:40 PM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Mar 2013
    Posts
    5

    Re: enum versus private enum

    Well it doesn't seem the type of cast matters, but using private enum as opposed to just enum does matter. I'm just wondering why. I'm going through Ivor Horton's Visual C++ 2010 book and ran into this error with one of the exercises. I searched google and found the "fix" but not the reason why it had to be *private* enum in VS2012 and enum was just fine in VS2010. Its just hard to believe one would have to rewrite such simple code from one version of VC++ to the next. I can imagine intricate code would have to be completely rewritten every time a new version of Visual studio comes out. I learned on unix with GNU C many years ago, and am just surprised at how messy that is.

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

This is a CodeGuru survey question.


Featured


HTML5 Development Center