Template Parameter Question

N

Nephi Immortal

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;
}
 
V

Victor Bazarov

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
 
N

Nephi Immortal

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.


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

"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?
 
V

Victor Bazarov

[..]
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
 
N

Nephi Immortal

[..]
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.
 
V

Victor Bazarov

[..]
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
 
J

James Kanze

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.
 
V

Victor Bazarov

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
 
P

Paul

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. ;-)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top