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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:45:07+00:00 2026-05-13T17:45:07+00:00

I have created a maths library that operates via templates, it allows the user

  • 0

I have created a maths library that operates via templates, it allows the user to specify the size and type of the array within a class which is then used to create a maths vector of any dimension up to four. As soon as I went to create a colour class, it struck me how similar the vector and colour class are. Is there anyway in which I could reduce code reuse and use some form of inheritance or specialisation to separate:

  1. Specific functionality (ie vector3 does not have a setXYZW() function, instead only a setXYZ()) to the dimension of which it can only be used in.
  2. Colour class and vector class can both (in terms of array data member) be of size ranging from 1 to 4 and the both share the same operators, but differ in their use in some circumstances such as a multiply vector differs from a multiply colour.

My knowledge of templates is not that good, so I would very much appreciate if anyone can show me the best solution to such a situation?

template < std::size_t N = 3, typename T = float >
class Vector
{
  typedef T Degree, Radian;
private:
  T m_vecEntry[N];
public:
  // arithmetic operations
  Vector operator + (const Vector & _vector) const;
  Vector operator - (const Vector & _vector) const;
  Vector operator * (const Vector & _vector) const;
  Vector operator * (float _val) const;
};

template < std::size_t N = 4, typename T = float >
class Colour
{
private:
  T m_colEntry[N];
public:
  // arithmetic operations
  Colour operator + (const Colour& _colour) const;
  Colour operator - (const Colour& _colour) const;
  Colour operator * (const Colour& _colour) const;
  Colour operator * (float _val) const;
};

  • 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-13T17:45:07+00:00Added an answer on May 13, 2026 at 5:45 pm

    Your classes have a fair amount of duplicated code, it is advisable that you do something about it. A possible solution follows.

    First, you take the common functionality to a base class:

    template <class Derived, std::size_t N, typename T>
    class VectorBase
    {
    protected:
      VectorBase() {} // Prevent instantiation of base
    
      Derived operator + (const Derived & _vector) const {
          std::cout << "Base addition\n";
          return Derived();
      }
      Derived operator * (T _val) const {
          std::cout << "Base scalar multiplication\n";
          return Derived();
      }
    
      T m_components[N];
    };
    

    Then you derive from it your Vector and Colour classes. In each derived class you use using Base::operation; to state explicitly that the corresponsing operation from the base class makes sense in the derived class.

    For operations that don’t make sense in the derived class you provide an alternative definition or not provide it at all (it will not be accessible since you didn’t write using).

    You can also add operations that were not in the base class, like Vector::norm:

    template < std::size_t N = 3, typename T = float >
    class Vector : VectorBase<Vector<N, T>, N, T>
    {
      typedef VectorBase<Vector<N, T>, N, T> Base;
      typedef T Degree, Radian;
    
    public:
      using Base::operator+; // Default implementation is valid
      using Base::operator*; // Default implementation is valid
      T norm() const {   // Not present in base class
          return T();
      }  
    };
    
    template < std::size_t N = 4, typename T = float >
    class Colour : VectorBase<Colour<N, T>, N, T>
    {
      typedef VectorBase<Colour<N, T>, N, T> Base; 
    public:
      using Base::operator+; // Default implementation is valid
    
      Colour operator * (T _val) const {  // Redefines version in base class
          std::cout << "Colour scalar multiplication\n";
          return Colour();
      }
    };
    

    The only trick in this code is that I’ve used the CRTP to make base class operations work with derived types.

    Here is a little test program:

    int main()
    {
      Vector<> va, vb;
      va + vb;
      va.norm();
      va * 3.0;
    
      Colour<> ca, cb;
      ca + cb;
      ca * 3.0f;
    }
    

    It prints:

    Base addition
    Base scalar multiplication
    Base addition
    Colour scalar multiplication
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a PHP-script to update a web server that is live inside
I have created a UserControl that has a ListView in it. The ListView is
I have created a few small flash widgets that stream .mp3 audio from an
i have created a workflow activity that do give the item creater of a
I'm writing a simple maths library with a template vector type: template<typename T, size_t
I have a reasonably advanced (many patches and subpatches) quartz composition that was created
Have created a c++ implementation of the Hough transform for detecting lines in images.
I have created a template for Visual Studio 2008 and it currently shows up
I have created a custom dialog for Visual Studio Setup Project using the steps
I have created a C# class file by using a XSD-file as an input.

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.