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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:40:46+00:00 2026-05-16T08:40:46+00:00

Edit : Note that my final purpose here is not having the class working,

  • 0

Edit:
Note that my final purpose here is not having the class working, is just learning more about templates 🙂

Suppose you have a template class which implements a vector:

template <typename T>
class Vector
{
    public:
        Vector(size_t dim) {
            dimension = dim;
            elements = new T[dim];
        }
        /* Here more stuff, like operator[] etc... */
    private:
        size_t dimension;
        T * elements;
}

And suppose you want to build a matrix with it. A matrix is just a vector of vectors, thus it can be designed as follows:

template <typename T>
class Matrix : public Vector<Vector<T> >
{
    /*...*/
}

And here comes trouble: In the constructor I need to provide rows and columns as parameter to the internal vectors. It should be something like

template <typename T>
Matrix<T>::Matrix (size_t ncols, size_t nrows)
         : Vector<Vector<T> > /* Here I need to specify size for both
                               * internal and external vectors */
{
}

Obviously I cannot write Vector<Vector<T>(nrows)>(ncols), but that’s what I would need!

A possible solution would be including size inside the template:

template <typename T, size_t N>
class Vector
{
    public:
        Vector() {
            elements = new T[N];
        }
        /* Here more stuff, like operator[] etc... */
    private:
        T * elements;
}

Hence I would no longer need constructor parameters, but this also forces me to write clumsy code with templates everywhere (by exmample, every function using a Vector should be declared as

template <typename T, size_t N>
void foo (Vector<T,N> &vec) {...}

Do you have better solutions?

EDIT:

As solution I took inspiration from Mr Fooz’s and chubsdad’s posts. That’s how I fixed the problem:

/* The RowAccess class is just an array wrapper which raises an exception
 * if you go out of bounds */
template <typename T>
class RowAccess
{
    public:

        RowAccess (T * values, unsigned cols) : values(vals), cols(c) {}

        T & operator[] (unsigned i) throw (MatrixError) {
            if (i < cols) return values[i];
            else throw MatrixError("Column out of bound");
        }

    private:
        T * values;
        unsigned cols;
};

template <typename T>
class Matrix
{
    public:
        Matrix (unsigned rows, unsigned cols) {...}
        virtual ~Matrix () {...}

        RowAccess<T> operator[] (unsigned row) {
            if (row < rows) return RowAccess<T>(values + cols * row, cols);
            else throw MatrixError("Row out of boundary");
        }

    private:
        unsigned rows;
        unsigned cols;
        T * values;
};

Thanks a lot to everyone!

  • 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-16T08:40:46+00:00Added an answer on May 16, 2026 at 8:40 am

    This isn’t what you asked, but there’s a good chance the matrix would be better of implemented as a single linear vector where you provide high-level access methods that do the indexing (e.g. elmLoc=row*ncols+col). This way you don’t need to create and initialize a vector of vectors. You also don’t need to worry about accidentally having some inner vectors of the differing size. All dense matrix implementations I have ever used use a single linear vector as the underlying implementation.

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

Sidebar

Ask A Question

Stats

  • Questions 518k
  • Answers 518k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If they are on same PC then yes, shared memory… May 16, 2026 at 8:06 pm
  • Editorial Team
    Editorial Team added an answer select pilot from pilotgroup pg inner join southoperation sop on… May 16, 2026 at 8:06 pm
  • Editorial Team
    Editorial Team added an answer From the documentation of Hibernate Validator: 6.1. Database schema-level validation… May 16, 2026 at 8:06 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Note that I'm a complete SQL noob and in the process of learning. Based
Here is a sample piece of code. Note that B is a subclass of
You may have noticed that we now show an edit summary on Community Wiki
I have a class which looks something like this: public class Test { private
Edit: Since it appears nobody is reading the original question this links to, let
I am forced to execute a periodic task using a timer that is invoked
I ran into my first compiler that changes the lvalue passed to ::delete, but
My friends and I are working on some basic Ruby exercises to get a
EDIT: had to retag this, because, it's rather a Sweave / R question since
I have an Activity with an AutoComplete box on it. Whenever the text changes,

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.