Strong vs Weak typed languages. Is it possible in c++ to point to variable type?

I want to have a data member that points to any of a number of predefined types. How is this possible?

Say if I want it to point to an int I can set it to point to an int, and I can have a function which returns the type of a parameter etc. Is that possible also?


Thanks in advance.


Edit: I just compiled the following code.... to my surprise it compiled.
But what does it do actually?
1
2
3
4
template <class itemType> itemType foo(itemType instance)
{
    return instance.itemType;
}
More precisely this is the code that I compiled.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

template <class itemType> itemType foo(itemType instance)
{
    return instance.itemType;
}

int main()
{
    int x = 3;
    //cout << foo(x);
    //foo(x);


    cout << "Program End.\n";
    return 0;
}
No, there's nothing akin to pointers to types in C++. There might be alternative solutions, but that requires knowing what you're trying to do.

But what does it do actually?


Your function foo takes a parameter of an arbitrary type and returns an object of the same type, which instance.itemType will be converted to if possible.
well what i was trying to do was change the implementation of my matrix class, which has a datamember int mymatrix[MAXSIZE][MAXSIZE]; as a container and int myRows and int myCols for size parameters. problem is, to use a simple 3x3 matrix, you have to create a MAXSIZExMAXSIZE matrix in each constructor, which is time consuming, especially for repetetive calls.

I was thinking of having something like 3 datamembers, and a pointer to one of those 3 datamembers.

something like:
1
2
3
4
5
6
7
8
9
public: 
typedef itemType mat2[2][2], mat3[3][3], mat4[4][4];
mat2 * m2, mat3 * m3, mat4 * m4;
private:
//something like:
//pointer pointer mymatrix;
//i.e.
* * mymatrix;

That way in order to keep the functionalizy of resizing a matrix later, you woudln't have to necessarily create a maxsize x maxsize sized matrix even for creating a small matrix...
Last edited on
Your matrix class should not have a fixed number of rows and columns. Use some dynamic memory allocation. This will take a little careful indexing since you cannot dynamically allocate a variable-sized multi-dimensional array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
template <typename T>
class matrix
  {
  public:
    typedef T value_type;

    matrix( unsigned rows = 0, unsigned cols = 0 ):
      nrows ( rows ),
      ncols ( cols ),
      data  ( (rows && cols) ? (new value_type[ nrows * ncols ]) : NULL )
      { }

    unsigned rowcount() const { return nrows; }
    unsigned colcount() const { return ncols; }

    value_type& operator () ( unsigned row, unsigned col )
      {
      if ((!data)
      or  (row >= nrows)) throw std::out_of_range( "matrix::operator( unsigned row, unsigned col ): row out of range" );
      if  (col >= ncols)  throw std::out_of_range( "matrix::operator( unsigned row, unsigned col ): col out of range" );

      return data[ (row * ncols) + col ];
      }

    const value_type& operator () ( unsigned row, unsigned col ) const
      {
      return this->operator () ( row, col );
      }

  private:
    unsigned    nrows;
    unsigned    ncols;
    value_type* data;
  };

You can easily add resize() and mathematical functionalities to all of this.

Hope this helps.
Topic archived. No new replies allowed.