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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:01:36+00:00 2026-05-22T15:01:36+00:00

#include <iostream> using namespace std; class Array { friend ostream &operator<<( ostream &, const

  • 0
#include <iostream>
using namespace std;

class Array
{
   friend ostream &operator<<( ostream &, const Array & );
public:
   Array( int = 5 ); 
   Array( const Array & ); 
   ~Array(); 
   int getSize() const; 
   const Array &operator=( const Array & );
   // subscript operator for non-const objects returns modifiable lvalue
   int &operator[]( int );              
   // subscript operator for const objects returns rvalue
   int operator[]( int ) const;  
private:
   int size; 
   int *ptr; 
}; 

Array::Array( int arraySize )
{
   size = ( arraySize > 0 ? arraySize : 5 ); // validate arraySize
   ptr = new int[ size ]; 

   for ( int i = 0; i < size; i++ )
      ptr[ i ] = 0; 
}

// must receive a reference to prevent infinite recursion
Array::Array( const Array &arrayToCopy ) 
   : size( arrayToCopy.size )
{
   ptr = new int[ size ]; // create space for pointer-based array

   for ( int i = 0; i < size; i++ )
      ptr[ i ] = arrayToCopy.ptr[ i ]; // copy into object
} 

Array::~Array()
{
   delete [] ptr; // release pointer-based array space
} 

int Array::getSize() const
{
   return size; // number of elements in Array
} 

const Array &Array::operator=( const Array &right )
{
   if ( &right != this ) // avoid self-assignment
   {
      if ( size != right.size )
      {
         delete [] ptr; // release space
         size = right.size; // resize this object
         ptr = new int[ size ]; // create space for array copy
      }

      for ( int i = 0; i < size; i++ )
         ptr[ i ] = right.ptr[ i ]; // copy array into object
   }

   return *this; 
}

// overloaded subscript operator for non-const Arrays reference return creates a modifiable lvalue
int &Array::operator[]( int subscript )
{
cout << " ***************Inside non-sonstant operator[] function: Lvalue test*********** ";
   if ( subscript < 0 || subscript >= size )
   {
      cerr << "\nError: Subscript " << subscript 
         << " out of range" << endl;
      exit( 1 ); // terminate program; subscript out of range
   }

   return ptr[ subscript ]; // reference return
}

// overloaded subscript operator for const Arrays const reference return creates an rvalue
int Array::operator[]( int subscript ) const
{
cout << " ***************Inside sonstant operator[] function: Rvalue test*********** ";
   if ( subscript < 0 || subscript >= size )
   {
      cerr << "\nError: Subscript " << subscript 
         << " out of range" << endl;
      exit( 1 ); 
   } 

   return ptr[ subscript ]; // returns copy of this element
}


// overloaded output operator for class Array 
ostream &operator<<( ostream &output, const Array &a )
{
   int i;
   // output private ptr-based array 
   for ( i = 0; i < a.size; i++ )
   {
      output << a.ptr[ i ] << " ";

      if ( ( i + 1 ) % 4 == 0 )
         output << endl;
   } // end for

   if ( i % 4 != 0 ) 
      output << endl;

   return output; 
} 

int main()
{
   Array integers1( 4 ); 
   Array integers2; // 5-element Array by default
   const Array& integers4=integers1;
    //integers4[3] = 2000; //Error : non-lvalue in assignment 
    integers1 = integers1;  //valid
    integers4 = integers1;  //Error : binary '=' : no operator found 
    //which takes a left-hand operand of type 'const Array' (or there is no 
    //acceptable conversion)
    cout << "\nintegers1[3] is " << integers4[ 3 ];

   return 0;
} 

Gives errors :

1)
In function `int main()’:

‘=’ : left operand must be l-value

2)
binary ‘=’ : no operator found
which takes a left-hand operand of type ‘const Array’ (or there is no
acceptable conversion)

Please help.

  • 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-22T15:01:37+00:00Added an answer on May 22, 2026 at 3:01 pm

    You cannot modify a const reference, Try this:

    //Snippet1
    Array& integers4=integers1;
    integers4[3] = 2000; 
    

    To clarify OP’s doubt about modifying const references:

    //Snippet2
    //This will not compile as you saw.
    const Array& integers4=integers1;
    integers4[3] = 2000; //errors
    

    Now this is what Xeo did in his reply to this post. He did not change the reference, but instead the original variable.

    //Snippet2
    //This will compile and work identically to Snippet1.
    const Array& integers4=integers1;
    interger1[3] = 2000; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include<iostream> using namespace std; class A { private: const int a=9; public: void display()
#include<iostream> using namespace std; class A { int a; int b; public: void eat()
#include <iostream> #include <vector> using namespace std; class base { int x; public: base(int
#include <iostream> using namespace std; class Base { public: Base(){cout <<Base<<endl;} virtual ~Base(){cout<<~Base<<endl;} virtual
#include <iostream.h> using namespace std; class A { public: virtual char* func()=0; }; class
#include<iostream> #include<string> using namespace std; class Prerequisites { public: void orderClasses(string* Input); }; void
#include<iostream> using namespace std; class base { public: virtual void add() { cout <<
#include <iostream> #include <vector> #include <algorithm> using namespace std; class A { public :
#include<iostream> using namespace std; class complex { double real; double image; public: complex(double r=0,double
code: #include<iostream> using namespace std; template<class T, int N> class point { T coordinate[N];

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.