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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:06:53+00:00 2026-05-20T10:06:53+00:00

For School I have been given a class interface to define my own array

  • 0

For School I have been given a class interface to define my own array class (not inherited).

I’m having trouble with overloading the assignment operator (=) and comparison operator (==) as well as the index operator.

This was given to me in the class interface:

Array& operator = (Array const& array1);   // overload assignment operator
bool operator == (Array const& array1);    // overload == operator

int const& operator [] (unsigned int index) const;   // overload index operator
int& operator [] (unsigned int index);      // overload index operator

What I can’t seem to understand is how to assign it. If the array that you want to assign to your new variable is passed to the function = how do you return the value of that array to assign it the new variable. Or do I just allocate a new memory location and it automatically assigns it to what called the function?

With the == operator, how do I know what it’s comparing to? How can I compare values, I don’t know how to reference what called the function?

I pretty much have no idea how to overload the index operator. If the variable that is using the index operator points to the first int in the array, how do I increment it to return the value of the index that was called?

Not to mention, I’m not sure what exactly we’re overloading it for, and the two overloading of the two index operators look the same. I don’t know what to do for them.

My teacher told me the assignment operator is just like the copy constructor, which I wrote as this:

Array::Array(Array const& oldarray)
{
   int *arraycopy;
   arraycopy = new int[oldarray.length] // length is a member variable that has already been initiated
   for(int i = 0; i < oldarray.length; i++) {
      arraycopy[i] = oldarray[i];
   }
}

Is it just the same thing for the assignment operator? I’m confused with all this and I will really appreciate any and all help! THanks!

  • 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-20T10:06:53+00:00Added an answer on May 20, 2026 at 10:06 am

    First and foremost it’s important you understand that the operators you mentioned work on this. Each give you different variables depending on the operator you choose to overload:

    Assignment operator:

    Array& operator = (Array const& array1);
    

    This operator means “assign array1 to this“, it is also known as “copy operator”.
    Normally it would be implemented by copying array1 into this depending on the data type. This can be achieved using memcpy for basic arrays.

    Comparison operator:

    bool operator == (Array const& array1);
    

    Again, this means “compare this to array1“. It should be pretty straight forward – use whatever logic the data type is using to determine if the data in this is equal to the data in array1. This can be achieved using memcmp for a basic array type.

    Index operators

    int& operator [] (unsigned int index);
    int const& operator [] (unsigned int index) const;
    

    Those are basically the same operator, however one of them is “read only” version where you cannot write to the array.

    This operator may be confusing, because it does not mean “put item at index”, but it means “give me a reference to index where I can put my item”. This means that the assignment is done externally, e.g, the statement:

    myarray[idx] = item;
    

    Is actually two statements (note: this assumes your Array class is an Array of ints):

    // Reference to the index location
    int& writeLocation = myarray[idx];
    
    // Write item to the location
    writeLocation = item;
    

    The implementation of this operator varies upon the implementation of your data type.

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

Sidebar

Related Questions

I'm working with a Ruby project for school, and have sadly not been able
At school we have been programming in MIPS assembly language for some time. I'm
I have been learning C++ with some books from school that are from the
Up till now I have been developing my personal and school projects at home
Okay, so far, I have been taking computer science courses in my high school
Over the years as I have gone through school and been working in the
I currently have a school assignment that involves both PHP and asp.net. Now the
For a school assignment I have to write x86 assembly code, except I can't
For a database assignment I have to model a system for a school. Part
EDIT: I have found the error: I did not initialize an array with a

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.