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 7754121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:13:42+00:00 2026-06-01T12:13:42+00:00

I’m trying to create a small set of classes implementing a safe flag pattern,

  • 0

I’m trying to create a small set of classes implementing a “safe flag” pattern, which would allow only predefined values to be OR-ed together. I’ve came up with something like that:

class MyClass;

class CreateFlag
{
friend class MyClass;

private:
    int value;

    CreateFlag(int newValue)
    {
        value = newValue;
    }

public:
    CreateFlag operator | (const CreateFlag & right) const
    {
        int newValue = value | right.value;
        return CreateFlag(newValue);
    }

    CreateFlag(const CreateFlag && flag)
    {
            value = flag.value;
    }

    CreateFlag(const CreateFlag & flag)
    {
        value = flag.value;
    }

    static const CreateFlag Flag1;
    static const CreateFlag Flag2;
    static const CreateFlag Flag4;
};

const CreateFlag CreateFlag::Flag1 = CreateFlag(1);
const CreateFlag CreateFlag::Flag2 = CreateFlag(2);
const CreateFlag CreateFlag::Flag4 = CreateFlag(4);

class MyClass
{
public:
    static void DisplayFlag(CreateFlag flag)
    {
        printf("Flag value: %d\n", flag.value);
    }
};

int main(int argc, char * argv)
{
    MyClass::DisplayFlag(CreateFlag::Flag1 | CreateFlag::Flag2);

    getchar();
}

The problem is, that CreateFlag has a private ctor (what is intentional), so there is no way to specify the values of CreateFlag’s static fields and the above code does not compile.

A way to bypass this restriction is to change static fields into static methods returning an instance of CreateFlag, but that’s a dirty solution, as you would have to call the metod like:

MyClass::DisplayFlag(CreateFlag::Flag1() | CreateFlag::Flag2());

Is there a direct solution? If it changes anything, the flag definitions may be as well moved to the MyClass class.

I know also, that C++11 supports in-place initialization of static fields, but unfortunatelly VC++10 doesn’t support this construct yet…

Edit I’ve modified the code, such that anyone can copy it and use, it compiles now and works as intended to.

  • 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-01T12:13:44+00:00Added an answer on June 1, 2026 at 12:13 pm

    The above code doesn’t compile, but I think that it’s not compiling for reasons beyond what you’re expecting. Specifically, the compile error is in this line:

    MyClass::DisplayFlag(CreateFlag::Flag1 | CreateFlag::Flag2);
    

    The reason is that DisplayFlag takes its CreateFlag argument by value, and you’ve marked the copy constructor private.

    If you’re trying to ensure that people can’t OR together invalid values, I don’t think you need the copy constructor to be private. Making copies of CreateFlags that you know are valid doesn’t allow clients to do anything that they previously couldn’t do. If you omit the definition of the copy constructor and just let C++’s default copy support work for you, you should be just fine.

    The lines that you’ve indicated as causing the error don’t seem to cause any problems. It compiles just fine once you remove the copy constructor.

    Hope this helps!

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I would like to run a str_replace or preg_replace which looks for certain words
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported

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.