Thread: Help with templates!

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    44

    Help with templates!

    i was reading a code from the book i study from and i saw that it appeared a variable, or i dont know what that is named "_m" this was never declared, and it apeared only once in the whole code. I tryed to compile it and it compiled succesfully. What does _m mean? here is the piece of code where it appears:
    Code:
    template <typename T>
    void Table<T>::destroy()
    {
         // Does the matrix exist?
         if( mDataMatrix )
         {
              for(int i = 0; i < _m; ++i)
              {
                   // Does the ith row exist?
                   if(mDataMatrix[i] )
                   {
                        // Yes, delete it.
                        delete[]mDataMatrix[i];
                        mDataMatrix[i] = 0;
                   }
              }
              // Delete the row-array.
              delete[] mDataMatrix;
              mDataMatrix = 0;
         }
    mNumRows = 0;
    mNumCols = 0;
    }
    and also, the compiler gives me a warning saying that the way i declared a function was obsolete. i decalred it as follows:
    Code:
    template <typename T>
    Table<T>::Table<T>()
    and the compiler says that insted of that i should compile it as:
    Code:
    template <typename T>
    Table<T>::Table()
    is it the same? or is it better the way the compiler tells me to do?

    thank you,
    Cherry.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In general, try to fix compiler warnings - they are there for a reason - and if it is kind enough to give you direct instructions on how to fix it, great!


    My guess, but I don't know for sure, is that _m is the number of rows in the Table object.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Code:
    Table<T>::Table()
    This means that there is a function Table and it is in the Table<T> scope. Table<T> is the class. Table is the function. Now if you say:
    Code:
    Table<T>::Table<T>()
    Then you mean there is a Table<T> function in the Table<T> class scope? But Table won't take T as a template argument. It doesn't need it.

    In other words it is a function (constructor) inside a template class, not a template function inside a template class.

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    matsp i know what does _m represent, but what i dont know is why does that compile without errors if _m was never defined...

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    _m is either a member of Table<T>, or a global (well, unlikely).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    Oh, it didnt compile right, i dont know why but before i posted i dont know what happened and compiled. i changed _m for mNumRows and now works fine =D

    but now i have one more dude...what does const mean in this cases?:
    Code:
    int numRows()const;
    int numCols()const;

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    const means that the this pointer that is implicitly passed to all member functions is constant.

    As a result this method cannot modify any class members (unless they are mutable) nor call any other non-constant methods.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    thank you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates from DLL or static library problem
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2008, 01:49 AM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  3. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 02:46 AM
  4. When and when not to use templates
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 07-20-2003, 09:36 AM