![]() |
Template Parameter Question
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; } |
Re: Template Parameter Question
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 |
Re: Template Parameter Question
On Mar 12, 8:35*am, Victor Bazarov <v.baza...@comcast.invalid> 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? |
Re: Template Parameter Question
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 |
Re: Template Parameter Question
On Mar 13, 8:40*am, Victor Bazarov <v.baza...@comcast.invalid> 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. |
Re: Template Parameter Question
On 3/13/2011 5:41 PM, Nephi Immortal wrote:
> On Mar 13, 8:40 am, Victor Bazarov<v.baza...@comcast.invalid> 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 |
Re: Template Parameter Question
On Mar 13, 1:40 pm, Victor Bazarov <v.baza...@comcast.invalid> 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 |
Re: Template Parameter Question
On 3/14/2011 6:29 PM, James Kanze wrote:
> On Mar 13, 1:40 pm, Victor Bazarov<v.baza...@comcast.invalid> 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 |
Re: Template Parameter Question
"Nephi Immortal" <immortalnephi@gmail.com> wrote in message news:ab3f618b-775d-436b-a947-53439c6a7cc6@x13g2000vbe.googlegroups.com... On Mar 13, 8:40 am, Victor Bazarov <v.baza...@comcast.invalid> 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. ;-) |
| All times are GMT. The time now is 02:22 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.