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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T21:32:09+00:00 2026-05-28T21:32:09+00:00

I have a couple of pure virtual classes, Matrix and Vector . Throughout my

  • 0

I have a couple of pure virtual classes, Matrix and Vector. Throughout my code base I try to only create dependencies on them and not their concrete subclasses e.g. SimpleTransformationMatrix44 and SimpleTranslationVector4. The motivation for this is that I can use third party (adapted) classes in place of mine without much trouble.

I would like to overload the arithmetic operators (sourced from here):

T T::operator +(const T& b) const;
T T::operator -(const T& b) const;
T T::operator *(const T& b) const;

I want to declare them in the pure virtual classes so that it is valid to perform the operations on references/pointers to them, the problem being that an abstract class cannot be returned by value. The best solution I can think of is something like this:

std::unique_ptr<T> T::operator +(const T& b) const;
std::unique_ptr<T> T::operator -(const T& b) const;
std::unique_ptr<T> T::operator *(const T& b) const;

Which allows this (without down casts!):

std::unique_ptr<Matrix> exampleFunction(const Matrix& matrix1, const Matrix& matrix2)
{
    std::unique_ptr<Matrix> product = matrix1 * matrix2;
    return std::move(product);
}

A pointer seems to be the only option in this case since returning a value is invalid and returning a reference is just plain silly.

So I guess my question is: Have I gone off the plot with this idea? If you saw it in some code you were working on would you be thinking WTF to yourself? Is there a better way to achieve this?

  • 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-28T21:32:10+00:00Added an answer on May 28, 2026 at 9:32 pm

    First off: Overloading operators is something that applies best to value types. As you have found out, polymorphism doesn’t play well with it. If you are willing to walk on crutches, this might help, though:

    If you follow the advice of Stackoverflow’s operator overloading FAQ, you will implement operator+() as a non-member atop of operator+=(). The latter returns a reference. It’s still a problem, because it can only return a base class reference, but as long as you only use it for expressions expecting that, you are fine.

    If you then templatize operator+() as DeadMG suggested, you might be able to do what you want:

    template<typename T>
    T operator+(const T lhs, const T& rhs)
    {
      lhs += rhs;
      return lhs;
    }
    

    Note that this would catch any T for which no better-matching operator+() overload can be found. (This might seem like a good idea — until you forget to include a header and this operator catches the x+y, making the code compile, but silently produce wrong results.) So you might want to restrict this.

    One way would be to put it into the same namespace as you matrix and vector types. Or you use a static_assert to ensure only types derived from the two are passed in.

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

Sidebar

Related Questions

Possible Duplicate: Pure virtual destructor in C++ I have two classes: the abstract Game
I have an abstract class with a couple pure virtual functions, and one of
I have couple of questions about AS3 variables handling by AVM/compiler/scope .1. This code
I'll have couple of python functions I must interface with from the assembly code.
ok I have couple of .NET classes that I want to use in VBA.
I have the following hierarchy of classes class classOne { virtual void abstractMethod() =
I have couple of statements, the pseudo code would look something like this: insert
I have couple of input fields with the class link. All of them should
I have a couple of app engine urls that return pure JSON and this
I have couple of helper methods added to the existing classes, and i want

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.