C
coder_lol
Thanks everyone again for contributing to helping me clear C++
confusions. I did some serious reading on copy constructors and
assignments and I think I've got a good handle on the memory stuff.
Well, I came across Scott Meyer's SmartPtr example from some 10 years
ago. I like the template member function for type conversion to solve
inheritance issues.
On MSVS 2003, I get the below error, if I declare the constructor
SmartPtr(T* realPtr = 0); as explicit, ie.
explicit SmartPtr(T* realPtr = 0);
SmartPtr.h(33): error C2520: SmartPtr<T>::SmartPtr: no non-explicit
constructor available for implicit conversion
with
[
T=Object
]
and
[
T=Object
]
I think of explicit as an "insurance" against silly mistakes. Is
there a way to solve the issue and keep explicit?
Thanks...
=== Scott Meyer's===
template<class T> // template class for smart
class SmartPtr { // pointers-to-T objects
public:
SmartPtr(T* realPtr = 0);
T* operator->() const;
T& operator*() const;
template<class newType> // template function for
operator SmartPtr<newType>() // implicit conversion ops.
{
return SmartPtr<newType>(pointee);
}
confusions. I did some serious reading on copy constructors and
assignments and I think I've got a good handle on the memory stuff.
Well, I came across Scott Meyer's SmartPtr example from some 10 years
ago. I like the template member function for type conversion to solve
inheritance issues.
On MSVS 2003, I get the below error, if I declare the constructor
SmartPtr(T* realPtr = 0); as explicit, ie.
explicit SmartPtr(T* realPtr = 0);
SmartPtr.h(33): error C2520: SmartPtr<T>::SmartPtr: no non-explicit
constructor available for implicit conversion
with
[
T=Object
]
and
[
T=Object
]
I think of explicit as an "insurance" against silly mistakes. Is
there a way to solve the issue and keep explicit?
Thanks...
=== Scott Meyer's===
template<class T> // template class for smart
class SmartPtr { // pointers-to-T objects
public:
SmartPtr(T* realPtr = 0);
T* operator->() const;
T& operator*() const;
template<class newType> // template function for
operator SmartPtr<newType>() // implicit conversion ops.
{
return SmartPtr<newType>(pointee);
}