Template Parameter Question

Discussion in 'C++' started by Nephi Immortal, Mar 11, 2011.

  1. Please confirm if template definition is valid. It is the class
    body. You may find the missing, but I don’t see the reason to add the
    missing since C++ Compiler does not generate an error message. I
    should have added A< T, T2 > in each member functions.
    If I insert or remove template parameters on top of class, then I do
    not need to modify each member functions. The alternative option is
    to use typedef.
    If I choose to explicit member function definitions outside the class
    body, then I have to put template< typename ….> and class<…> on each
    memer functions. Typedef is not helpful.

    template< typename T, typename T2 >
    class A {
    public:
    A( T t, T2 t2 ) : m_t( t ), m_t2( t2 ) {
    }

    ~A() {
    }

    A( const A &r ) : m_t( r.m_t ), m_t2( r.m_t2 ) {
    }

    A &operator=( const A &right ) {
    m_t = right.m_t;
    m_t2 = right.m_t2;
    return *this;
    }

    /*
    A< T, T2 >( T t, T2 t2 ) : m_t( t ), m_t2( t2 ) {
    }

    ~A< T, T2 > () {
    }

    A< T, T2 > ( const A< T, T2 > &r ) : m_t( r.m_t ), m_t2( r.m_t2 ) {
    }

    A< T, T2 > &operator=( const A< T, T2 > &right ) {
    m_t = right.m_t;
    m_t2 = right.m_t2;
    return *this;
    }
    */
    private:
    T m_t;
    T2 m_t2;
    };


    int main() {
    A< char, int > a( 1, 2 ), a2( 3, 4 );
    a = a2;

    return 0;
    }
     
    Nephi Immortal, Mar 11, 2011
    #1
    1. Advertising

  2. On 3/11/2011 4:52 PM, Nephi Immortal wrote:
    > Please confirm if template definition is valid. It is the class
    > body. You may find the missing, but I don’t see the reason to add the
    > missing since C++ Compiler does not generate an error message. I
    > should have added A< T, T2> in each member functions.


    What?!

    The name of the class template ('A' in your case) is implicitly declared
    in the class template definition, and means that type. For instance in
    the definition of template<int a, class T> class Foo, the symbol 'Foo'
    means the same thing as 'Foo<a,T>'.

    If that's what you're asking, there is your answer. If it's not, make
    yourself clearer.

    > If I insert or remove template parameters on top of class, then I do
    > not need to modify each member functions. The alternative option is
    > to use typedef.


    Do you have "C++ Templates" book by Vandevoorde and Josuttis? If yes,
    use it. If not, get it, then use it.

    > If I choose to explicit member function definitions outside the class
    > body, then I have to put template< typename ….> and class<…> on each
    > memer functions. Typedef is not helpful.
    >
    > template< typename T, typename T2>
    > class A {
    > public:
    > A( T t, T2 t2 ) : m_t( t ), m_t2( t2 ) {
    > }
    >
    > ~A() {
    > }
    >
    > A( const A&r ) : m_t( r.m_t ), m_t2( r.m_t2 ) {
    > }
    >
    > A&operator=( const A&right ) {
    > m_t = right.m_t;
    > m_t2 = right.m_t2;
    > return *this;
    > }
    >
    > /*
    > A< T, T2>( T t, T2 t2 ) : m_t( t ), m_t2( t2 ) {
    > }
    >
    > ~A< T, T2> () {
    > }
    >
    > A< T, T2> ( const A< T, T2> &r ) : m_t( r.m_t ), m_t2( r.m_t2 ) {
    > }
    >
    > A< T, T2> &operator=( const A< T, T2> &right ) {
    > m_t = right.m_t;
    > m_t2 = right.m_t2;
    > return *this;
    > }
    > */
    > private:
    > T m_t;
    > T2 m_t2;
    > };
    >
    >
    > int main() {
    > A< char, int> a( 1, 2 ), a2( 3, 4 );
    > a = a2;
    >
    > return 0;
    > }


    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 12, 2011
    #2
    1. Advertising

  3. On Mar 12, 8:35 am, Victor Bazarov <> wrote:
    > On 3/11/2011 4:52 PM, Nephi Immortal wrote:
    >
    > >    Please confirm if template definition is valid.  It is the class
    > > body.  You may find the missing, but I don t see the reason to add the
    > > missing since C++ Compiler does not generate an error message.  I
    > > should have added A<  T, T2>  in each member functions.

    >
    > What?!
    >
    > The name of the class template ('A' in your case) is implicitly declared
    > in the class template definition, and means that type.  For instance in
    > the definition of template<int a, class T> class Foo, the symbol 'Foo'
    > means the same thing as 'Foo<a,T>'.
    >
    > If that's what you're asking, there is your answer.  If it's not, make
    > yourself clearer.
    >
    > >    If I insert or remove template parameters on top of class, then I do
    > > not need to modify each member functions.  The alternative option is
    > > to use typedef.

    >
    > Do you have "C++ Templates" book by Vandevoorde and Josuttis?  If yes,
    > use it.  If not, get it, then use it.
    >
    >
    >
    >
    >
    > >    If I choose to explicit member function definitions outside the class
    > > body, then I have to put template<  typename .>  and class< >  oneach
    > > memer functions.  Typedef is not helpful.

    >
    > > template<  typename T, typename T2>
    > > class A {
    > > public:
    > >    A( T t, T2 t2 ) : m_t( t ), m_t2( t2 ) {
    > >    }

    >
    > >    ~A() {
    > >    }

    >
    > >    A( const A&r ) : m_t( r.m_t ), m_t2( r.m_t2 ) {
    > >    }

    >
    > >    A&operator=( const A&right ) {
    > >            m_t = right.m_t;
    > >            m_t2 = right.m_t2;
    > >            return *this;
    > >    }

    >
    > > /*
    > >    A<  T, T2>( T t, T2 t2 ) : m_t( t ), m_t2( t2 ) {
    > >    }

    >
    > >    ~A<  T, T2>  () {
    > >    }

    >
    > >    A<  T, T2>  ( const A<  T, T2>  &r ) : m_t( r.m_t ), m_t2( r.m_t2 ) {
    > >    }

    >
    > >    A<  T, T2>  &operator=( const A<  T, T2>  &right ) {
    > >            m_t = right.m_t;
    > >            m_t2 = right.m_t2;
    > >            return *this;
    > >    }
    > > */
    > > private:
    > >    T m_t;
    > >    T2 m_t2;
    > > };

    >
    > > int main() {
    > >    A<  char, int>  a( 1, 2 ), a2( 3, 4 );
    > >    a = a2;

    >
    > >    return 0;
    > > }


    "C++ Templates" book by Vandevoorde and Josuttis

    Ok. I will buy C++ Template book. You have recognized “How to
    program C++” by Deitel provides a very little information about
    template.

    I want to learn how to write overloading operators by the following
    technique rules including memory management and smart pointer. Also,
    I can write vector and iterate from scratch for practical purpose
    before I can start to use STL.. Which book do you recommend?
     
    Nephi Immortal, Mar 12, 2011
    #3
  4. On 3/12/2011 11:01 AM, Nephi Immortal wrote:
    >[..]
    > I want to learn how to write overloading operators by the following
    > technique rules including memory management and smart pointer. Also,
    > I can write vector and iterate from scratch for practical purpose
    > before I can start to use STL.. Which book do you recommend?


    C++ Templates by Vandevoorde and Josuttis, Modern C++ Design by
    Alexandrescu.

    And, honestly, I don't understand the need to "write vector and iterate
    from scratch" before starting to use "STL". The Standard library is
    there to be used, and in order to use it correctly you *don't need* to
    write one from scratch yourself.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 13, 2011
    #4
  5. On Mar 13, 8:40 am, Victor Bazarov <> wrote:
    > On 3/12/2011 11:01 AM, Nephi Immortal wrote:
    >
    > >[..]
    > > I want to learn how to write overloading operators by the following
    > > technique rules including memory management and smart pointer.  Also,
    > > I can write vector and iterate from scratch for practical purpose
    > > before I can start to use STL..  Which book do you recommend?

    >
    > C++ Templates by Vandevoorde and Josuttis, Modern C++ Design by
    > Alexandrescu.
    >
    > And, honestly, I don't understand the need to "write vector and iterate
    > from scratch" before starting to use "STL".  The Standard library is
    > there to be used, and in order to use it correctly you *don't need* to
    > write one from scratch yourself.


    You asked a good question. Writing vector class from scratch help
    you to learn how to write C++ code in a better design. It is just for
    exercise practices as self-study. After you learn writing the code,
    you become to be advanced programmer and joins with other programmers
    to enhance better STL and Boost designs in the future.
    You will have to code thousands of classes. Put them together to
    become component or library or software development.
    I focus studying the template and define better overloading operator
    designs. I hope you understand what I mean.
     
    Nephi Immortal, Mar 13, 2011
    #5
  6. On 3/13/2011 5:41 PM, Nephi Immortal wrote:
    > On Mar 13, 8:40 am, Victor Bazarov<> wrote:
    >> On 3/12/2011 11:01 AM, Nephi Immortal wrote:
    >>
    >>> [..]
    >>> I want to learn how to write overloading operators by the following
    >>> technique rules including memory management and smart pointer. Also,
    >>> I can write vector and iterate from scratch for practical purpose
    >>> before I can start to use STL.. Which book do you recommend?

    >>
    >> C++ Templates by Vandevoorde and Josuttis, Modern C++ Design by
    >> Alexandrescu.
    >>
    >> And, honestly, I don't understand the need to "write vector and iterate
    >> from scratch" before starting to use "STL". The Standard library is
    >> there to be used, and in order to use it correctly you *don't need* to
    >> write one from scratch yourself.

    >
    > You asked a good question.


    Actually, I didn't ask a question.

    > Writing vector class from scratch help
    > you to learn how to write C++ code in a better design. It is just for
    > exercise practices as self-study. After you learn writing the code,
    > you become to be advanced programmer and joins with other programmers
    > to enhance better STL and Boost designs in the future.
    > You will have to code thousands of classes. Put them together to
    > become component or library or software development.


    Where did this propaganda come from? Your boss or your professor? You
    don't have to first learn metallurgy to become good at using a steel
    hammer. You don't need to become a good gardener, a miller, or a
    butcher to be a good cook. Snap out of it!

    > I focus studying the template and define better overloading operator
    > designs. I hope you understand what I mean.


    No, I don't understand what you mean. Moreover, I don't think you
    actually *mean* it. I think you're repeating some mantra you heard from
    a person of authority, without actually thinking about it.

    That's all.

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 14, 2011
    #6
  7. Nephi Immortal

    James Kanze Guest

    On Mar 13, 1:40 pm, Victor Bazarov <> wrote:
    > On 3/12/2011 11:01 AM, Nephi Immortal wrote:


    > >[..]
    > > I want to learn how to write overloading operators by the following
    > > technique rules including memory management and smart pointer. Also,
    > > I can write vector and iterate from scratch for practical purpose
    > > before I can start to use STL.. Which book do you recommend?


    > C++ Templates by Vandevoorde and Josuttis, Modern C++ Design by
    > Alexandrescu.


    I'm not sure I'd cite those two books in the same sentence. The
    Vandevoorde and Josuttis is a must if you want to develop
    templates; there are few better books. (I don't know of any.)
    The Alexandrescu is interesting if you want to explore just how
    far you can go, but there's almost nothing in it that you would
    want to use in production code: it's more a collection of
    advanced ideas, to experiment with.

    --
    James Kanze
     
    James Kanze, Mar 14, 2011
    #7
  8. On 3/14/2011 6:29 PM, James Kanze wrote:
    > On Mar 13, 1:40 pm, Victor Bazarov<> wrote:
    >> On 3/12/2011 11:01 AM, Nephi Immortal wrote:

    >
    >>> [..]
    >>> I want to learn how to write overloading operators by the following
    >>> technique rules including memory management and smart pointer. Also,
    >>> I can write vector and iterate from scratch for practical purpose
    >>> before I can start to use STL.. Which book do you recommend?

    >
    >> C++ Templates by Vandevoorde and Josuttis, Modern C++ Design by
    >> Alexandrescu.

    >
    > I'm not sure I'd cite those two books in the same sentence. The
    > Vandevoorde and Josuttis is a must if you want to develop
    > templates; there are few better books. (I don't know of any.)
    > The Alexandrescu is interesting if you want to explore just how
    > far you can go, but there's almost nothing in it that you would
    > want to use in production code: it's more a collection of
    > advanced ideas, to experiment with.


    And that's why they complement each other so well! Why would I
    recommend books that are interchangeable? <shrug>

    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 14, 2011
    #8
  9. Nephi Immortal

    Paul Guest

    "Nephi Immortal" <> wrote in message
    news:...
    On Mar 13, 8:40 am, Victor Bazarov <> wrote:
    >You asked a good question. Writing vector class from scratch help
    >you to learn how to write C++ code in a better design. It is just for
    >exercise practices as self-study. After you learn writing the code,
    >you become to be advanced programmer and joins with other programmers
    >to enhance better STL and Boost designs in the future.
    >You will have to code thousands of classes. Put them together to
    >become component or library or software development.
    >I focus studying the template and define better overloading operator
    >designs. I hope you understand what I mean.



    I understand what you mean and I agree with you .
    A really good blacksmith knows how to make a good hammer. ;-)
     
    Paul, Mar 24, 2011
    #9
    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. Chris Theis
    Replies:
    2
    Views:
    493
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    556
    tom_usenet
    Jul 24, 2003
  3. Gianni Mariani
    Replies:
    1
    Views:
    361
    tom_usenet
    Sep 5, 2003
  4. Stuart Redmann
    Replies:
    5
    Views:
    505
    Stuart Redmann
    Dec 14, 2007
  5. Isti
    Replies:
    2
    Views:
    745
Loading...

Share This Page