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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:39:57+00:00 2026-05-13T22:39:57+00:00

I have the following tricky problem: I have implemented a (rather complicated) class which

  • 0

I have the following tricky problem: I have implemented a (rather complicated) class which represents mathematical functions in a multiwavelet basis. Since operations like +, – and * are quite natural in this context, I have implemented overloaded operators for this class:

FunctionTree<D> operator+(FunctionTree<D> &inpTree);
FunctionTree<D> operator-(FunctionTree<D> &inpTree);
FunctionTree<D> operator*(FunctionTree<D> &inpTree);

There operators work finer in simple, non-chained operations, and even in some cases when chaining the operators. Statements like

FunctionTree<3> y = a * b + c;
FunctionTree<3> z = a * b + b;

compile and seemingly work fine. The first line is actually ok, but the second line causes valgrind to tell you grim stories about memory being freed inside already freed regions and uninitialized variables being accessed. Furthermore, a statement like

FunctionTree<D> y = a + b * c;

will not even compile, because I have not defined (an ambiguous operator taking an actual object, not a reference, as argument). Of course the solution is clear: All arguments, and methods ought to be made const, and perhaps even return a const object or reference. Unfortunately this is not possible, since NONE of the objects involved are constant during the operations! This may sound strange, but this is an unavoidable consequence of the mathematics involved. I can fake it, using const_cast, but the code is still wrong!

Is there a way to solve this problem? The only solution I have currently is to make the return objects const, thus effectively disabling the operator chaining.

Regards, .jonas.

  • 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-13T22:39:57+00:00Added an answer on May 13, 2026 at 10:39 pm

    You can use proxies instead of real values, and proxies can be constant, as they are not going to be changed. Below is a small example of how it might look like. Be aware that all the temporaries are still going to be created in that example but if you want to be smart, you can just save the operations, not the actual results of operations, and calculate only when someone wants to finally get result or a part of it. It might even speed up your code enormously as it helped APL

    Also, you might want to make most of the members private.

    #include <memory>
    #include <iostream>
    
    struct FunctionTreeProxy;
    struct FunctionTree;
    
    struct FunctionTreeProxy {
        mutable std::auto_ptr<FunctionTree> ft;
    
        explicit FunctionTreeProxy(FunctionTree * _ft): ft(_ft) {}
        FunctionTreeProxy(FunctionTreeProxy const & rhs): ft(rhs.ft) {}
    
        FunctionTreeProxy operator+(FunctionTree & rhs);
        FunctionTreeProxy operator*(FunctionTree & rhs);
        FunctionTreeProxy operator+(FunctionTreeProxy const & rhs);
        FunctionTreeProxy operator*(FunctionTreeProxy const & rhs);
    };
    
    struct FunctionTree {
        int i;
        FunctionTree(int _i): i(_i) {}
        FunctionTree(FunctionTreeProxy const & rhs): i(rhs.ft->i) {}
    
        FunctionTree * add(FunctionTree & rhs) {
            return new FunctionTree(i + rhs.i);
        }
    
        FunctionTree * mult(FunctionTree & rhs) {
            return new FunctionTree(i * rhs.i);
        }
    
        FunctionTreeProxy operator+(FunctionTree & rhs) {
            return FunctionTreeProxy(add(rhs));
        }
    
        FunctionTreeProxy operator*(FunctionTree & rhs) {
            return FunctionTreeProxy(mult(rhs));
        }
    
        FunctionTreeProxy operator+(FunctionTreeProxy const & rhs) {
            return FunctionTreeProxy(add(*rhs.ft));
        }
    
        FunctionTreeProxy operator*(FunctionTreeProxy const & rhs) {
            return FunctionTreeProxy(mult(*rhs.ft));
        }
    };
    
    FunctionTreeProxy FunctionTreeProxy::operator+(FunctionTree & rhs) {
        return FunctionTreeProxy(ft.get()->add(rhs));
    }
    
    FunctionTreeProxy FunctionTreeProxy::operator*(FunctionTree & rhs) {
        return FunctionTreeProxy(ft.get()->mult(rhs));
    }
    
    FunctionTreeProxy FunctionTreeProxy::operator+(FunctionTreeProxy const & rhs) {
        return FunctionTreeProxy(ft.get()->add(*rhs.ft));
    }
    
    FunctionTreeProxy FunctionTreeProxy::operator*(FunctionTreeProxy const & rhs) {
        return FunctionTreeProxy(ft.get()->mult(*rhs.ft));
    }
    
    int main(int argc, char* argv[])
    {
        FunctionTree a(1), b(2), c(3);
    
        FunctionTree z = a + b * c;
    
        std::cout << z.i << std::endl;
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.