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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:38:52+00:00 2026-06-09T19:38:52+00:00

I am trying to implement a simple multi-dimensional Point class with templates (learning). I

  • 0

I am trying to implement a simple multi-dimensional Point class with templates (learning). I need two specializations Point2D and Point3D – here is what I have got so far to allow for constructors to initialize the Point directly like Point p (1, 2). Although this code compiles and works fine, what I don’t like is the code repetition part in the specialization – I must be doing something wrong.

I am new to C++ / templates – any help is appreciated.

#ifndef POINT_H_
#define POINT_H_

template< typename T, int Dimensions = 2 >
class Point
{
public:
    typedef typename T value_type;

    Point() { std::fill(elements_, elements_+Dimensions, 0); }
    Point(const Point<T, Dimensions>& rhs) : elements_(rhs.elements_) {}
    ~Point() {}


    Point & operator=(const Point<T, Dimensions>& rhs) { return *this; }

    const Point operator+(const Point<T, Dimensions>& p)
    {
        Point<T, Dimensions> ret;

        for(int i = 0; i < Dimensions; i++)
        {
            ret[i] += elements_[i] + p[i];
        }

        return ret;
    }

    Point & operator+=( const Point<T, Dimensions>& p)
    {
        for(int i = 0; i < Dimensions; i++)
        {
            elements_[i] += p[i];
        }

        return *this;
    }

    Point & operator-=( const Point<T, Dimensions> & p)
    {
        for(int i = 0; i < Dimensions; i++)
        {
            elements_[i] -= p[i];
        }

        return *this;
    }

    T & operator[](const size_t index)
    {
        return elements_[index];
    }

private:
    T elements_[Dimensions];
};

template<typename T>
class Point< T, 2 >
{
public:
    Point(const T x, const T y)
    {
        elements_[0] = x;
        elements_[1] = y;
    }

    typedef typename T value_type;

    Point() { std::fill(elements_, elements_+Dimensions, 0); }
    Point(const Point<T, 2>& rhs) : elements_(rhs.elements_) {}
    ~Point() {}


    Point & operator=(const Point<T, 2>& rhs) { return *this; }

    const Point operator+(const Point<T, 2>& p)
    {
        Point<T, 2> ret;

        for(int i = 0; i < 2; i++)
        {
            ret[i] += elements_[i] + p[i];
        }

        return ret;
    }

    Point & operator+=( const Point<T, 2>& p)
    {
        for(int i = 0; i < 2; i++)
        {
            elements_[i] += p[i];
        }

        return *this;
    }

    Point & operator-=( const Point<T, 2> & p)
    {
        for(int i = 0; i < 2; i++)
        {
            elements_[i] -= p[i];
        }

        return *this;
    }

    T & operator[](const size_t index)
    {
        return elements_[index];
    }

private:
    T elements_[2];
};


template< typename T>
class Point< T, 3 >
{
public:

    Point(const T x, const T y, const T z)
    {
        elements_[0] = x;
        elements_[1] = y;
        elements_[2] = z;
    }

    typedef typename T value_type;

    Point() { std::fill(elements_, elements_+3, 0); }
    Point(const Point<T, 3>& rhs) : elements_(rhs.elements_) {}
    ~Point() {}


    Point & operator=(const Point<T, 3>& rhs) { return *this; }

    const Point operator+(const Point<T, 3>& p)
    {
        Point<T, 3> ret;

        for(int i = 0; i < 3; i++)
        {
            ret[i] += elements_[i] + p[i];
        }

        return ret;
    }

    Point & operator+=( const Point<T, 3>& p)
    {
        for(int i = 0; i < 3; i++)
        {
            elements_[i] += p[i];
        }

        return *this;
    }

    Point & operator-=( const Point<T, 3> & p)
    {
        for(int i = 0; i < 3; i++)
        {
            elements_[i] -= p[i];
        }

        return *this;
    }

    T & operator[](const size_t index)
    {
        return elements_[index];
    }

private:
    T elements_[3];
};

typedef Point< int, 2 > Point2Di;
typedef Point< int, 3 > Point3Di;


#endif //POINT_H_
  • 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-09T19:38:54+00:00Added an answer on June 9, 2026 at 7:38 pm

    You can just – simply – provide both a 2D and a 3D constructor in the main template.

    There is no need to dally with base classes and other Rube Goldberg solutions here, because there is no problem to be solved: we’re in template-land, where anything unused is simply unused.

    Example:

    #ifndef POINT_H_
    #define POINT_H_
    
    #include <array>            // std::array
    
    #define STATIC_ASSERT( e ) static_assert( e, "!(" #e ")" )
    
    template< typename T, int nDimensions = 2 >
    class Point
    {
    private:
        std::array< T, nDimensions > elements_;
    
    public:
        typedef T ValueType;
    
        T& operator[]( int const i )
        {
            return elements_[i];
        }
    
        T const& operator[]( int const i ) const
        {
            return elements_[i];
        }
    
        void operator+=( Point const& other )
        {
            for( int i = 0; i < nDimensions; ++i )
            {
                elements_[i] += other.elements_[i];
            }
        }
    
        void operator-=( Point const& other )
        {
            for( int i = 0; i < nDimensions; ++i )
            {
                elements_[i] -= other.elements_[i];
            }
        }
    
        friend Point operator+( Point const& a, Point const& b )
        {
            Point ret( a );
    
            ret += b;
            return ret;
        }
    
        friend Point operator-( Point const&a, Point const& b )
        {
            Point ret( a );
    
            ret -= b;
            return ret;
        }
    
        Point(): elements_() {}
    
        Point( int x, int y )
        {
            STATIC_ASSERT( nDimensions == 2 );
            elements_[0] = x;
            elements_[1] = y;
        }
    
        Point( int x, int y, int z )
        {
            STATIC_ASSERT( nDimensions == 3 );
            elements_[0] = x;
            elements_[1] = y;
            elements_[2] = z;
        }
    };
    
    typedef Point< int, 2 > Point2D;
    typedef Point< int, 3 > Point3D;
    
    #endif //POINT_H_
    
    #include <iostream>
    using namespace std;
    
    wostream& operator<<( wostream& stream, Point3D const& point )
    {
        return (stream << "(" << point[0] << ", " << point[1] << ", " << point[2] << ")");
    }
    
    int main()
    {
        wcout << "starting" << endl;
        Point3D a( 1, 2, 3 );
        Point3D b( 4, 5, 6 );
    
        a += b;
        wcout << a << endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement simple sliding window alogirithm on two-dimensional array in C# 3.0,
I am trying to implement a simple digital clock with second and two colons
I am trying to implement a simple Observer pattern using .net Observable class. I
I'm trying to implement simple Event Bus I started like this: public class RegistrationData
I'm trying to implement a simple messaging system in CakePHP 1.3. What I need
I'm trying to implement simple webprofiler using QtWebKit (by extending Ghost.py). I need to
I've been trying to implement simple low level keyhook using JNI and all went
I'm getting acquanted with Core Animation and trying to implement simple movement along a
Iam trying to implement a simple JMS(traditional not using springs) code in eclipse using
Just trying to implement a simple audit logging solution with Grails 2.0.2 and it

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.