Click to See Complete Forum and Search --> : Can we add two templates in c++
mayank_khare
December 2nd, 2004, 01:54 AM
Hi guys
I have curiosity in my mind about templates.I just want to know that can we add two templates like we add two objects in c++.If we can add templates, then what is the procedure to add them.Plz tell me.
Thanks & Regards
Mayank Khare
Kheun
December 2nd, 2004, 02:50 AM
If you are refering the adding two template class instances where the template parameters are the same, it will be exaclty the same as adding two normal class instances that are of the same type. If the class provides an operator+(), then addition can be performed.
wien
December 2nd, 2004, 09:26 AM
Additionally (no pun intended), if you template the + operator itself, you can enable addition of different template instances also. Whether or not it would make sense to do so however, depends on the context.
NMTop40
December 3rd, 2004, 06:06 AM
You can write a template default function for operator+ which makes use of += thus:
template < typename L, typename R >
L operator+( L l, const R& r )
{
l += r;
return l;
}
The first parameter could be const L& but then you'd have to invoke the copy constructor inside the body. This way does it automatically.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.