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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:44:32+00:00 2026-05-23T19:44:32+00:00

I have made the following code:- class A{ bool bFlag[2]; public: A(){ for(int i

  • 0

I have made the following code:-

class A{
     bool bFlag[2];
public:
  A(){
      for(int  i = 0; i < 2; i++)
          bFlag[i] = false;
  }

  bool operator[](int r){ //i know how to assign value to R.H.S using operator[]
      if( r >= 0 || r < 2 ){
          bFlag[r] = true;
          return bFlag[r];
   }
   return false;        
  }   
};

int main(){
    A obj;
    bool x;
    x = obj[0]; //this i know
    //obj[1] = x; //how to do this is my doubt?
    return 0;
}

I dont know to set value to L.H.S using operator[].
Please guide me to how to set x value to obj[1]

  • 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-23T19:44:32+00:00Added an answer on May 23, 2026 at 7:44 pm

    Als is right, but here is an example:

    class A{
    private:
        int val[10];
    public:
        A(){}
        int& operator[](int i) {
            return val[i];
        }
    };
    

    this makes in posible to do things like

    A a;
    a[2] = 2;
    

    you should of course add the old method as well. to optimize for l-value to making it.

    class A{
    private:
        int val[10];
    public:
        A(){}
        const int operator[](int i) const {
            return val[i];
        }
        int& operator[](int i) {
            return val[i];
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that contains the following two properties: public int Id {
I have roughly the following code. Could this be made nicer or more efficient?
I have made a new windows service which works fine using barebone code (just
Consider the following code parts: this is Timing.h: class Timing { public: //creates a
Consider the following code: using System; using System.Runtime.InteropServices; namespace Demo { class Program {
I have made a CheckedListBox using the following: <ListBox x:Name=lst_checkBoxList ItemsSource={Binding}> <ListBox.ItemTemplate> <DataTemplate> <CheckBox
Long story short Say I have the following code: // a class like this
I have the following object: namespace LearnLINQ1 { [Table(Name=testMe)] public class SubmitTest { [Column(Name=FirstName)]
I have an application that is made up of the following: A central database
I have made some code which exports some details of a journal article 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.