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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:15:04+00:00 2026-06-12T16:15:04+00:00

I have this class to factorize common operations on N-dimensional vector spaces: template <unsigned

  • 0

I have this class to factorize common operations on N-dimensional vector spaces:

template <unsigned int N>
struct BaseVector
{
    float m_data[N];
    // Common operations like dot product, magnitude, test for unity, etc.
};

Note: I really want to factorize as much code as possible to minimize the amount of documentation and testing.

Now, I derive two classes:

// 3D geometric vectors
struct Vector3 : public BaseVector<3>
{
    Vector3 Cross(const Vector3& other);
    // ...
};

// Quaternions are a particular kind of vector space
struct Quaternion : public BaseVector<4>
{
    void Interpolate(const Quaternion& start, const Quaternion& end, ...);
    // ...
};

These classes behave similarly for addition and scalar multiplication (component-wise operation); so, I want to factorize operator+=() and operator*=() in the base vector class.

My question is: How do I return a reference of the proper type?

template <unsigned int N>
struct BaseVector
{
    ??? & operator+=(const BaseVector& other)
    {
        transform(m_data, m_data+N, other.m_data, m_data, plus<float>());
        return ???
    }
};

All my ideas so far (listed bellow) are not satisfying and I would appreciate some suggestions, thanks!


Idea #1: Use the C++ covariant return type mechanism. But then, I must overload these operators in the derived classes–am I right? (Which means duplicate testing to me.)

Idea #2: Go for templates?

template <unsigned int N>
struct BaseVector
{
    template <typename T2>
    T2 & operator+=(const T2& other)
    {
        transform(...);
        return *this; // THIS IS WRONG! I'm trying to "upcast"
    }
};

Idea #3: Factorize code into a private member in the base vector, but then I have to add more functions in derived classes (and more stuff to test)

template <unsigned int N>
struct BaseVector
{
private:
    void DoOperatorPlus(const BaseVector& other) { transform(...); }
};

struct Vector4 : public BaseVector<4>
{
    Vector4& operator+=(const Vector4& other)
    {
        DoOperatorPlus(other);
        return *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-06-12T16:15:05+00:00Added an answer on June 12, 2026 at 4:15 pm

    You could actually try to use CRTP

    The idea is that you give a template parameter to your base class of your derived class:

        template <unsigned int N, typename Derived>
        struct BaseVector
        {
            Derived & operator+=(const Derived& other)
            {
                transform(m_data, m_data+N, other.m_data, m_data, plus<float>());
                return static_cast<Derived&>(*this);
            }
        };
    

    I am not 100% sure about the return statement but this should give you an idea.

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

Sidebar

Related Questions

I have this class: Class B { private String D; private String E; }
I have this class file call SMSHelper.cs First I just wanted to know is
I have this class in which I run a for loop 10 times. This
I have this class which i would like to map: public class Contract {
I have this class: class OriginalClass { string FirstItem; List<string> ListOfSecondItems; } I want
i have this class public class Image { public string url { get; set;
i have this class called MemoryManager, it is supposed to implement a simple smart
I have this class: class View(object): def main_page(self, extra_placeholders = None): file = '/media/Shared/sites/www/subdomains/pypular/static/layout.tmpl'
I have this class: public static class CsvWriter { private static StreamWriter _writer =
I have this class called SiteAsyncDownload.cs Here's the code: public class SiteAsyncDownloader { WebClient

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.