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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:06:25+00:00 2026-05-24T23:06:25+00:00

Coming from a PHP background, I’m trying to learn C++, since I find it

  • 0

Coming from a PHP background, I’m trying to learn C++, since I find it an interesting language. As a practice I want to create a simple Vector class using templates, which is not to hard. However, I run into one problem.

I have created the following template class:

template <typename T>
class Vector
{
public:
    Vector(int length);
    ~Vector(void);
    int getLength();

    T& operator[] (const int index);

private:
    T *_items;
    int _count;
};


template <typename T>
Vector<T>::Vector(int length)
{
    _items = new T[length];
    _count = length;
}

template <typename T>
T& Vector<T>::operator[]( const int index )
{
    if (index >= getLength() || index < 0)
        throw exception("Array out of bounds.");
    return _items[index];
}

All functions are implemented, but they’re not relevant to my question, so I haven’t copied them here.

This class works as expected, with one exception:
If I want to create a vector of array’s, it doesn’t work.
e.g.:

Vector<int[2]> someVector(5);

What I obviously want is that the _items property of the vector class will be an int[5][2]. However, since the compiler replaces the ‘T’ with ‘int[2]’, the _items property will be int[2][5] (or at least, that’s what I understood from debugging, please correct me if I’m wrong). As a result, the [] operator doesn’t work correctly anymore, and therefore this whole class is useless for arrays.

Is there a way to solve this problem, so that this class also works for arrays? And if that’s not possible, is there a way to prevent this class being initialized with arrays?

Edit: Thanks for all your responses so far. However, I might have been not entirely clear with my question. First of all, I created this class to get used to c++, I know there is a std::vector class, and I also now that it’s better to use a vector of vectors. That’s not really the problem. I just want to understand templates better, and c++ in general, so I want to know how to deal with this type of problems. I want to be able to create classes which don’t make the program crash. If I, or someone else, would use this class right now and tried to use primitive arrays instead of vectors for this class, at some point the program will crash since the array is wrong (Vector(y) becomes int[x][y] instead of int[y][x] internally). So I want a solution which either creates the correct array, or prevents the vector being initialized with arrays at all.

  • 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-24T23:06:26+00:00Added an answer on May 24, 2026 at 11:06 pm

    Quite simply, don’t ever use the in-built primitive arrays. For anything. They suck, tremendously. Always use a class wrapper such as boost::array, if you don’t have TR1 or C++0x both of which also provide an array class. A Vector<boost::array<int, 2>> will trivially compile.

    Why not use primitive arrays?

    They have hideous implicit conversions to pointers, forget their size at the drop of a hat, aren’t first-class citizens (can’t assign to them, for example), don’t check their bounds, for example. boost::array<int, 2> is exactly a primitive array without crappy conversions, is fully generic- for example, your Vector template works fine with a boost::array<int, 2> off the bat, and it can do bounds checking in the at() function.

    I’m not suggesting using a dynamic array instead. boost::array is not a dynamically sized array. It is a constant size, value type array, that will not convert to a pointer, it has functions for use in the Standard library like begin, end, size, and it can be treated like any other type- unlike arrays, which have a dozen special rules- as you’ve found out.

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

Sidebar

Related Questions

I´m trying to learn C#, coming from a Python/PHP background, and I´m trying to
I'm trying to learn xQuery coming from a php background, I have this expression
I'm beginning to learn some Hadoop/MapReduce, coming mostly from a PHP background, with a
Coming from a PHP background, I'm used to writing small functions that return a
I am coming from a PHP background and I am familiar with OOP concepts,
Coming from a PHP background, I am a little spoiled with the str_replace function
I'm a developer in Python coming from a PHP background. In PHP most frameworks
I'm having a heap of trouble with Java coming from a PHP background. I've
I'm completely new to Mongo and RoR, coming from a PHP background. I was
I am new to Java coming from a PHP background so sorry if this

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.