Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8771143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:42:45+00:00 2026-06-13T17:42:45+00:00

I had enum class, say enum class Enum{ var1, var2; } Now I want

  • 0

I had enum class, say

enum class Enum{
   var1, var2;
}

Now I want to add some member which depends on parameter i.e var3(int). OK, It’s not for enum, so I want to change it by regular class, but my goal is to leave old code(Enum::var1 as value of type Enum) possible to compile.

I tried to do it this way(let’s temporary forgot about var3, it’ll be static function):

class Enum{
    public:
        const static Enum var1 = Enum(1);
        const static Enum var2 = Enum(2);
    private:
        Enum(int v):v(v){
        }
    int v;
    //operator == using v
};

But it doesn’t compile because Enum has incomplete type.
I can’t declare it after class because it’s in header so it’ll not work with multiple cpp’s. Besides, it’s not great idea to have public constructor here.

Any thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T17:42:47+00:00Added an answer on June 13, 2026 at 5:42 pm

    Solution 1:

    For the static variable problem: declare your static variables in the class declaration:

    class Enum
    {
        public:
            static const Enum var1;
            static const Enum var2;
            Enum(int v):v(v)
            {
            }
        private:
            int v;
            //operator == using v
    };
    

    Then, create a source file for this class, Enum.cpp, containing:

    #include "Enum.h"
    const Enum Enum::var1 = Enum(1);
    const Enum Enum::var2 = Enum(2);
    

    Solution 2:

    If you want it to be header-only, you can use static variables instead of class variables:

    class Enum
    {
        public:
            Enum(int v):v(v)
            {
            }
        private:
            int v;
    };
    
    namespace Enumeration // It is not possible to name it 'Enum'
    {
        // static => local to translation unit. No linking conflict
        static const Enum var1 = Enum(1);
        static const Enum var2 = Enum(2);
    }
    

    You can see a live example here. The only drawback is that you cannot use the name of the class for the namespace.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had this question solved: Define "dynamic" enum Which I needed to get some
I'd like to write an extension method that extends some member of my class.
I have a class named Particle which has a std::set as a member. The
I am trying to get a custom enum class working which should enable me
I have problem with the declaration of enum in my class. I had tried
Had a page that was working fine. Only change I made was to add
Had a problem with the recursive conflictCheck() method. That seems fine now. I have
Had this working; at one stage. The problem is the following text is now
I had a debate about macros and their readability. I think that in some
For a simple action class with these member variables: ... private TestConverterEnum test; private

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.