5 Replies - 324 Views - Last Post: 27 December 2012 - 10:06 AM Rate Topic: -----

#1 lopidas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-October 12

Template based override

Posted 26 December 2012 - 01:42 PM

I am creating Vector class. I want to do it n-dimensional and I have to do it with STL array, not with STL vector
here is my code:
template<typename T,int Dimension>
class Vector{
public:
    Vector(T dim[Dimension])
    {
    int i;
    for(i=0;i<Dimension;i++)
    val[i]=dim[Dimension];
    };
private:
    array<T,Dimension> val;
};



Fine I want to do values x y z and w which will be synonyms for val[0,1,2,3] but i want them to be created only only if Dimension is bigger than 1 2 3 4. I want to override []

Something like T & operator []{return ...}

I haven't found anything.
Is this possible and how?

Is This A Good Question/Topic? 0
  • +

Replies To: Template based override

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,377
  • Joined: 31-December 10

Re: Template based override

Posted 26 December 2012 - 02:43 PM

View Postlopidas, on 26 December 2012 - 03:42 PM, said:

Fine I want to do values x y z and w which will be synonyms for val[0,1,2,3] but i want them to be created only only if Dimension is bigger than 1 2 3 4. I want to override []

Something like T & operator []{return ...}

I haven't found anything.
Is this possible and how?

Yes, it is possible to overload the subscript operator. First I would fix the errors in your code though. Here's your Vector class constructor:
Vector(T dim[Dimension])
    {
    int i;
    for(i=0;i<Dimension;i++)
    val[i]=dim[Dimension];
    };

First thing, which isn't a big deal, is that you could just declare the i variable inside the for-loop.

Let's say Dimension is 5, the only line in the for-loop is: val[i]=dim[Dimension];, if we try to access outside the bounds of the array, we're in trouble. So you have to remember that indexing starts at 0, therefore our array would have the indexes 0, 1, 2, 3, 4. Dimension is 5, so you try to access outside the array bounds in the line inside the for-loop.

That line inside the for-loop is wrong anyways, you want to copy each element in the dim array into your private array val: val[i]=dim[i];.

This post has been edited by vividexstance: 26 December 2012 - 02:46 PM

Was This Post Helpful? 0
  • +
  • -

#3 lopidas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-October 12

Re: Template based override

Posted 27 December 2012 - 08:48 AM

View Postvividexstance, on 26 December 2012 - 02:43 PM, said:

View Postlopidas, on 26 December 2012 - 03:42 PM, said:

Fine I want to do values x y z and w which will be synonyms for val[0,1,2,3] but i want them to be created only only if Dimension is bigger than 1 2 3 4. I want to override []

Something like T & operator []{return ...}

I haven't found anything.
Is this possible and how?

Yes, it is possible to overload the subscript operator. First I would fix the errors in your code though. Here's your Vector class constructor:
Vector(T dim[Dimension])
    {
    int i;
    for(i=0;i<Dimension;i++)
    val[i]=dim[Dimension];
    };

First thing, which isn't a big deal, is that you could just declare the i variable inside the for-loop.

Let's say Dimension is 5, the only line in the for-loop is: val[i]=dim[Dimension];, if we try to access outside the bounds of the array, we're in trouble. So you have to remember that indexing starts at 0, therefore our array would have the indexes 0, 1, 2, 3, 4. Dimension is 5, so you try to access outside the array bounds in the line inside the for-loop.

That line inside the for-loop is wrong anyways, you want to copy each element in the dim array into your private array val: val[i]=dim[i];.

I repaired that error before, but my question is still unanswared and I know I can override superscriptor operator and I doesn't access out of bounds I don't have <= there.
I want to do something like this pseudocode in constructor:

if(Dimension /*yes that of template*/>=1){declare Vector.private/*it is pseudocode*/.x=&val[0]}
if(Dimension /*yes that of template*/>=2){declare Vector.private/*it is pseudocode*/.y=&val[1]}
if(Dimension /*yes that of template*/>=3){declare Vector.private/*it is pseudocode*/.z=&val[2]}
if(Dimension /*yes that of template*/>=4){declare Vector.private/*it is pseudocode*/.w=&val[3]}

Is this possible declare private variable on construction if template value Dimnsion is specific number?
Was This Post Helpful? 0
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,377
  • Joined: 31-December 10

Re: Template based override

Posted 27 December 2012 - 09:09 AM

View Postlopidas, on 27 December 2012 - 10:48 AM, said:

I repaired that error before, but my question is still unanswared and I know I can override superscriptor operator and I doesn't access out of bounds I don't have <= there.

Yes you do access the array out of bounds when you do this: dim[Dimension], that is accessing the array out of bounds.

You can't declare a private class variable based off the number in Dimension. You could have a private class variable and assign it a value in the constructor based off the value of Dimension.
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,315
  • Joined: 21-June 11

Re: Template based override

Posted 27 December 2012 - 09:20 AM

You could add the variables to your class and then add specializations for Dimension 1, 2 and 3 without some of the variables (i.e. the specialization for Dimension=1 would only have an x variable etc.).
Was This Post Helpful? 1
  • +
  • -

#6 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 687
  • View blog
  • Posts: 2,377
  • Joined: 31-December 10

Re: Template based override

Posted 27 December 2012 - 10:06 AM

View Postsepp2k, on 27 December 2012 - 11:20 AM, said:

You could add the variables to your class and then add specializations for Dimension 1, 2 and 3 without some of the variables (i.e. the specialization for Dimension=1 would only have an x variable etc.).

Not sure why I didn't think of this, but then of course, depending on how high dimension goes, you may have many specializations.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1