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

  • Home
  • SEARCH
  • 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 8523073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:18:20+00:00 2026-06-11T07:18:20+00:00

I have a question about Operator Overloading in C++. For an assignment, I have

  • 0

I have a question about Operator Overloading in C++.

For an assignment, I have to write a class which encompasses an array, sort of like the ArrayList in Java.

One of the things I have to do is keep track of the size of the array. Size is the amount of elements included, whereas capacity is the maximum amount which CAN be included before the class has to expand the array.

Client code specifies the size when they call the constructor. However, when new elements are added, I have to figure out a way to change the size.

My teacher said something about being able to overload an operator for different sides of an equality. Is this a real thing, or did I misunderstand her? If this works, it would be the optimal solution to my problem.

My current overloading for the [] operator is:

int & ArrayWrapper::operator [] (int position){

if(position == _size){
    if(_size == _capacity){
        changeCapacity(_capacity+10);
    }
}
return _array[position];
}

This works fine for retrieval, but I’d like to have it so that if someone calls it from the left hand side of a ‘=’ then it checks to see if it needs to expand the size or not.

EDIT: If this isn’t a real thing, can anyone think of a different solution to the problem? One solution I thought of is to have the getSize() method just go through the entire array every time it is called, but I’d really rather not use that solution because it seems cheesy.

EDIT: For clarification, I’m not asking whether or not my expansion of an array works. I need to add 1 to size every time a new element is added. For example, if the client creates an array of size 15 and capacity 25, and then tries to add something to Array[15], that SHOULD increase the size to 16. I was wondering if there was a way to do that with overloading.

  • 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-11T07:18:22+00:00Added an answer on June 11, 2026 at 7:18 am

    A simple approach, which doesn’t quite do what you want, is to overload on whether the array is const or mutable.

    This doesn’t distinguish between whether the array is being used on the left-hand side of assignment (as a lvalue) or on the right (as a rvalue); just on whether it’s allowed to be modified or not.

    // Mutable overload (returns a mutable reference)
    int & operator[](size_t position) {
        if (position >= _size) {
            if (position >= _capatity) {
               // increase capacity
            }
            // increase size
        }
        return _array[position];
    }
    
    // Const overload (returns a value or const reference)
    int operator[](size_t position) const {
        if (position >= _size) {
            throw std::out_of_range("Array position out of range");
        }
        return _array[position];
    }
    

    If you really want to tell whether you’re being assigned to or not, then you’ll have to return a proxy for the reference. This overloads assignment to write to the array, and provides a conversion operator to get the value of the element:

    class proxy {
    public:
        proxy(ArrayWrapper & array, size_t position) :
            _array(array), _position(position) {}
    
        operator int() const {
            if (_position >= _array._array._size) {            
                throw std::out_of_range("Array position out of range");
            }
            return _array._array[_position];
        }
    
        proxy & operator=(int value) {
            if (_position >= _size) {
                if (_position >= _capatity) {
                    // increase capacity
                }
                // increase size
            }
            _array._array[_position] = value;
            return *this;
        }
    
    private:
        ArrayWrapper & _array;
        size_t _position;
    };
    

    You probably need to declare this a friend of ArrayWrapper; then just return this from operator[]:

    proxy ArrayWrapper::operator[](size_t position) {
        return proxy(*this, position);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about the return value of operator overloading in C++. Generally,
I have a question about the this operator in Java. In the case where
I have a question about what I think is an operator or argument passer
I have question about parsing in Html helper : I have sth like: @foreach
I have question about XSLT1.0. The task is to write out in HTML all
I have a question about how GC works in Java. Consider the following code:
I have a question about default = (equals) operator in F#. It allows to
I just have a quick question about the conditional operator. Still a budding programmer
I program in Java and have been trying to understand exactly what operator overloading
i'm not sure if what im talking about is an operator overloading question. is

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.