Re: template member function reference!

Discussion in 'C++' started by John Harrison, Jun 30, 2003.

  1. "Dimitris Trehas" <> wrote in message
    news:bdnkgh$1k7a$...
    > Hello to everybody!!
    >
    > I would like to ask if there is a way to get a reference to
    > a member function that is class templated! (wow english!)
    >
    > In order to make it clear
    >
    >
    > I have a matrix<T> class :
    >
    >
    >

    //--------------------------------------------------------------------------
    > -----------------------------------------------------------
    > template <typename T>
    >
    > class matrix {
    >
    > .
    >
    > .
    >
    > .
    >
    > T* dataPtr;
    >
    > T** rowsPtr;
    >
    >
    >
    > T& data(size_t i,size_t j ) { return rowsPtr[j];}
    >
    > };
    >
    >

    //--------------------------------------------------------------------------
    > -----------------------------------------------------------
    >
    > I want althought to have a transposedview of this matrix: (where aT(i,j) =
    > a(j,i))
    >
    >
    > So I wrote
    >

    //--------------------------------------------------------------------------
    > -----------------------------------------------------------
    > template <typename T>
    >
    > class matrix {
    >
    >
    >
    > .
    >
    > .
    >
    > .
    >
    > T* dataPtr;
    >
    > T** rowsPtr;
    >
    > public:
    >
    > T& data1(size_t i,size_t j ) { return rowsPtr[j];}
    >
    > T& data2(size_t i,size_t j ) { return rowsPtr[j];}
    >
    > T& (&data) (size_t i, size_t j); // or T& (*data (size_t i, size_t j);
    >
    > matrix() { data = data1;}
    >
    > };
    >
    >

    //--------------------------------------------------------------------------
    > -----------------------------------------------------------
    >
    >
    > This is not working! What I'm doing wrong?
    >
    > Thank U in advance,
    > Trehas Dimitris
    >


    Its possible, but I'm sure you don't want to do that

    class matrix {
    T* dataPtr;
    T** rowsPtr;
    public:
    T& data1(size_t i,size_t j ) { return rowsPtr[j];}
    T& data2(size_t i,size_t j ) { return rowsPtr[j];}
    T& (matrix<T>::* data)(size_t, size_t);
    matrix() { data = data1;}
    };

    int main()
    {
    matrix<int> x;
    (x.*x.data)(1, 2);
    }

    look at the way you have to use data! And public member variables are a bad
    idea. I think you should come up with a different design.

    john
    John Harrison, Jun 30, 2003
    #1
    1. Advertising

  2. Thanks John!

    My intention was to use a function (data) that It could be used the way I
    want to!

    like matrix<int> m;
    .....// load matrix with data...

    int mm = m.data(1,3);
    //...
    //... after all

    m.transposedView = true;

    int kk = m.data(3,1); /// So, the first argument is the column index and not
    the row index!!

    ///Internally I would like to have 2 functions and to choose between them
    without use if... else!!

    Thanks a lot!
    Dimitris Trehas, Jun 30, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave
    Replies:
    4
    Views:
    7,639
    pdixtl
    Jun 4, 2010
  2. Replies:
    1
    Views:
    463
    Victor Bazarov
    Jul 20, 2005
  3. Peng Yu
    Replies:
    3
    Views:
    752
    Thomas J. Gritzan
    Oct 26, 2008
  4. Hicham Mouline
    Replies:
    0
    Views:
    414
    Hicham Mouline
    Apr 23, 2009
  5. Hicham Mouline
    Replies:
    1
    Views:
    398
    Michael DOUBEZ
    Apr 24, 2009
Loading...

Share This Page