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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:04:28+00:00 2026-05-26T09:04:28+00:00

My class has a private enum whose members are being used to index an

  • 0

My class has a private enum whose members are being used to index an array of strings, the output of which is written to an output stream.

private:
    enum supportedMessageTypes(CRITICAL = 0, WARNING, INFORMATION);
    string messages[3];

//meanwhile, inside the constructor,
messages[3] = {"Critical error message", 
               "Warning message", 
               "Information message"};

Since I’m going to be using the enum values around my code a lot, I’d like to be able to overload operator<< to perform a lookup of the enum value, match it to the corresponding string in the array, and return that in the following manner:

cout << CRITICAL << ": " << messageText << std::endl;

The problem I have is that supportedMessageTypes is private while overloading of operator<< should be done as a non-member function. I know I can define a friend function that overloads operator<< for my class, but I’m simply not comfortable with breaking encapsulation in that way, and was wondering if anyone knew of a way to overload operator<< without using friend functions or making supportedMessageTypes public?

  • 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-05-26T09:04:29+00:00Added an answer on May 26, 2026 at 9:04 am

    This isn’t possible. You want to accept an argument of type supportedMessageTypes, so it must be visible. There’s no way around it.

    Also, a friend function wouldn’t be bad here; this is one of the intented scenarios for friendship.

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

Sidebar

Related Questions

I created a simple class in C++ which has a private dynamic array. In
I have a class which has two HashSet<String> collections as private members. Other classes
I have a class which has a private attribute which is a reference to
I have a class which has a private attribute vector rectVec; class A {
Let's say we have a class foo which has a private instance variable bar
If a class has a private constructor then it can't be instantiated. So, if
When creating a class that has internal private methods, usually to reduce code duplication,
I have a base class that has a private static member: class Base {
How do I use JUnit to test a class that has internal private methods,
When I create utility classes I typically create a class that has a 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.