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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:27:15+00:00 2026-05-22T19:27:15+00:00

i.e. if I define operators == and + in my class in a private

  • 0

i.e. if I define operators == and + in my class in a private section, can they be accessible from main?

It works in MSVS 2008 and 2010 but for me it seems to be a bug in a compiler. Is it so?

  • 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-22T19:27:17+00:00Added an answer on May 22, 2026 at 7:27 pm

    You will have to show the code to get a sensible explanation of why the compiler is accepting it. My guess is that you are implementing them as friend free functions. At any rate, for the sake of argument, assume you have:

    class bar {
       friend bool operator==( bar const &, bar const & ) {
          return true;
       }
       bar operator+( bar const & ) {
          return *this;
       }
    };
    
    int main() {
       bar a, b;
       a == b;    // ok
       //a + b;   // nok: operator+ is private from this context
    }
    

    And now the explanation. In the example, operator+ is declared as a member function inside a private section, as such, access specifiers apply and unless main is a friend of the class it will not have access to it. On the other hand operator== is implemented as a free function (even if the definition is provided inside the class braces) and access specifiers do not apply there.

    The code is almost equivalent (there is a small difference when it comes to lookup) to:

    class bar {
       friend bool operator==( bar const &, bar const & ); // just declare as friend
       //...
    };
    bool operator==( bar const &, bar const & ) {
       return true;
    }
    

    Where it is much simpler to reason about accessibility of operator== from the main function.

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

Sidebar

Related Questions

I have a template class which defines operators, that works on the template parameters.
// stock.h #ifndef STOCK_H #define STOCK_H // declare Stock Class class Stock { private:
I'm using the CPoint class from MFC. There is no explicitly defined assignment operator
Is it possible to define operators for enums? For example I have enum Month
Possible Duplicate: The written versions of the logical operators. I notice that C++ define
Can't define (?) operator overload on type: type Foo = val s : string
class A { private: int m_nValue; public: A() { m_nValue = 0; } A(int
I wrote class like this: #pragma once #include stdafx.h struct Date { private: int
In short: is there some way I can modify a class definition such that
I have a class CFoo with a private inner class CBar. I want to

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.