I have a container class that is templatized. I am overloading the assignment operator such that derived types can also be assigned.
My problem is, when the type is not the same, I cannot access the private members of the container class. What is the best approach to gaining access? The member variables cannot be made accessible through public getters. Thanks!
Example Code:
// Note: var is private
template <class T>
Container<T>& Container<T>::operator=(const Container<T>& rhs) {
if(*this != rhs) var = rhs.var; // works for same type
return *this;
}
template <class T>
template <typename U>
Container<T>& Container<T>::operator=(const Container<U>& rhs) {
if(*this != rhs) var = rhs.var; // does NOT work for different types
return *this;
}