advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Do you want your compiler to support exported templates in the future or is the current state of affairs, whereby a template's definition must appear in the header file, satisfactory? Let us know in the C++ Forum.
Partners & Affiliates
advertisement
advertisement
advertisement
advertisement
Average Rating: 5/5 | Rate this item | 1 user has rated this item.
Use an Asymmetric Assignment Operator to Assign Containers of Different Types (cont'd)
Definition
The precise implementation details of the assignment operator depend, of course, on how your template stores its elements. However, the general idea is to break down the collection to its individual elements, assigning every Array<T2>[n] to its matching Array<T>[n]. Here is a complete definition of the assignment operator you've just declared:

template <class T>
template <class T2>
Array<T>& Array<T>::operator=(const Array<T2>& t2)
{
if (this == (void*) &t2) //avoid self assignment
return *this;
clear(); //remove existing elements
for (size_t i=0; i<t2.size(); ++i)
v.push_back(t2[i]); //implicit conversion of T2 to T1
return *this;
}
advertisement
Pay attention to the first two lines of the definition. This part:

template <class T>
template <class T2>
indicates a definition of a member template of a class template.

Now, you can assign different Array specializations:


Array<int> a;
Array<double> d;
a.add_element(10);
a.add_element(20);
d=a; //OK, d contains the elements 10.0 and 20.0
The asymmetric assignment operator doesn't disable type safety. If you try to assign the following objects:

Array <string> words;
Array <int> num;
num=words; //compilation error
Your compiler will complain because there is no implicit string to int conversion.

Design Refinements
The compiler-generated assignment operator, not the asymmetric one, is still used when you assign objects of the same type:


Array<int> a1, a2;
a2=a1; //calls compiler-generated operator=
Remember: the asymmetric assignment operator is called only when the operands have different types. This discovery leads to a picking question. If the two operands have different types, don't they always have different addresses as well? In other words, isn't the code that checks for self-assignment redundant? In the overwhelming majority of cases, it is, indeed, redundant. Yet it's still possible to write code—however uncommon—that assigns the same object to itself using the asymmetric assignment operator. Here's an example:

a=*(Array<long>*)&a;
You wouldn't write such code. However, third-party libraries might contain gems like this. Therefore, the self-assignment test protects you from unpleasant runtime surprises.

Previous Page: Presenting the Problem  
Danny Kalev is a certified system analyst and software engineer specializing in C++ and the theoretical aspects of formal languages. He is the author of Informit C++ Reference Guide and The ANSI/ISO Professional C++ Programmer's Handbook. He was a member of the C++ standards committee between 1997 and 2000. Danny recently finished his MA in general linguistics summa cum laude. In his spare time he likes to listen to classical music, read Victorian literature, and explore natural languages such as Hittite, Basque, and Irish Gaelic. Additional interests include archeology and geology. He also gives lectures about programming languages and applied linguistics at academic institutes.
Page 1: IntroductionPage 3: Definition
Page 2: Presenting the Problem 
Please rate this item (5=best)
 1  2  3  4  5
advertisement