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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:15:06+00:00 2026-06-01T10:15:06+00:00

I’m writing an generic Array class and overloading operators for convenience. I’ve gotten my

  • 0

I’m writing an generic Array class and overloading operators for convenience. I’ve gotten my Array<> object to hold other Array<> objects, but I’m having trouble overriding the * operator. I need to copy the left object, so my operator* code is this:

Array<T>& operator*(const double scalar) {
    return Array<T>(*this) *= scalar;
}

(operator*= has been overloaded and works).

I overrode the copy constructor as below:

UPDATE: New copy constructor:

Array<T> (const Array<T>& copyfrom) {
    size_=copyfrom.size();
    data=new T[size_];
    copy(&copyfrom.data[0], &copyfrom.data[size_], data);
}

My thought is that if the array were not generic but always filled with a primitive, this code would work. But I think there’s something going on here because I’m using a template that’s causing behavior I don’t expect. The data array in the new “deep copied” Array acts like it’s just a shallow pointer copy of the “copyfrom” data array.

How do I made this copy constructor work for both primitives and objects, using templates? Or, better yet, is there a way to overload operator* without worrying about copy constructors? Thanks.

EDIT: Here is the code for operator*=. I still think my problem lies in my use of the copy constructor, however.

Array<T>& operator*=(const double scalar) {
    for (int i=0; i<size_; i++)
        data[i]*=scalar;
    return *this;
}

EDIT: I realized that I was getting problems because I was ignoring the size of my inner arrays in my array of arrays. Things are more reliable now. Everyone’s been really helpful, and I think I’m on the way to making this work. My operator= (which I hadn’t overloaded; good catch, Michael) is now as below. It acts as expected, but I’m starting to get malloc errors in my program and I’m exploring why. Is there anything wrong with the memory management here?

Array<T>& operator=(const Array<T>& a) {
    if (this==&a)
        return *this;
    delete [] data;

    size_=a.size();
    data=new T(size_);
    copy(&a.data[0], &a.data[a.size()], data);

    return *this;
}

EDIT: I fixed the malloc errors! My methods are all working as intended now. The memory problems were coming because I had this method header:

template <typename T>
static Array<T> matrixSolve (Array<Array<T> > m);

I was taking an array of arrays by value. All sorts of problems. Took the array by reference and everything worked out. Thanks for the help, everyone!

  • 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-01T10:15:08+00:00Added an answer on June 1, 2026 at 10:15 am

    You’ll want your operator* member function to look something like this:

    Array<T> operator*(const double scalar) const {
        Array<T> result(*this);
        result *= scalar;
        return result;
    }
    

    If you return an Array<T> &, then you will be returning a reference to a temporary which will be destroyed, causing undefined behavior.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the
i got an object with contents of html markup in it, for example: string

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.