| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| C++ (Non Visual C++ Issues) Ask or answer C and C++ questions not related to Visual C++. This includes Console programming, Linux programming, or general ANSI C++. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Implicit conversion operators
I've been playing around with a class that wraps around a fixed-size array (note this isn't for serious use). Because I want to be able to treat the Array objects basically like you would a normal stack allocated array, I've allowed the Array class to implicitly convert to a pointer.
The Array class: Code:
template<typename T, std::size_t size>
class Array
{
public:
const std::size_t elements;
explicit Array(const T& initialValue = T( ))
: elements(size)
{
std::fill(m_data, m_data + size, initialValue);
}
Array(const Array& copy)
: elements(size)
{
std::copy(copy.m_data, copy.m_data + size, m_data);
}
const Array& operator=(const Array& rhs)
{
std::copy(rhs.m_data, rhs.m_data + size, m_data);
return *this;
}
template<typename T, std::size_t otherSize>
Array(const Array<T, otherSize>& copy)
: elements(size)
{
const T* copyData = copy;
std::copy(copyData, copyData + (size < otherSize ? size : otherSize), m_data);
}
template<typename T, std::size_t otherSize>
const Array& operator=(const Array<T, otherSize>& rhs)
{
const T* rhsData = rhs;
std::copy(rhsData, rhsData + (size < otherSize ? size : otherSize), m_data);
return *this;
}
const T& operator[](std::size_t index) const
{
assert(index < size);
return m_data[index];
}
T& operator[](std::size_t index)
{
return const_cast<T&>(static_cast<const Array&>(*this)[index]);
}
operator const T*( ) const
{
return m_data;
}
operator T*( )
{
return m_data;
}
private:
T m_data[size];
};
Code:
Array<Array<int, 5>, 5> a; a[0][0] = 1; perator[], or to convert the array to a pointer and call built-in operator[]. Is there some way to get around the problem and prevent the conversion in this instance, or to force Array: perator[] to be called (without explicitly doing a.operator[](0).operator[](0))?
|
|
#2
|
|||
|
|||
|
Re: Implicit conversion operators
For you example, you should also provide the operator[]() instead of operator*(). Otherwise, the compiler will be confused with the native "float operator[](size_t)".
Code:
template<typename T, std::size_t size>
class Array
{
public:
const std::size_t elements;
explicit Array(const T& initialValue = T( ))
: elements(size)
{
std::fill(m_data, m_data + size, initialValue);
}
Array(const Array& copy)
: elements(size)
{
std::copy(copy.m_data, copy.m_data + size, m_data);
}
const Array& operator=(const Array& rhs)
{
std::copy(rhs.m_data, rhs.m_data + size, m_data);
return *this;
}
template<typename T, std::size_t otherSize>
Array(const Array<T, otherSize>& copy)
: elements(size)
{
const T* copyData = copy;
std::copy(copyData, copyData + (size < otherSize ? size : otherSize), m_data);
}
template<typename T, std::size_t otherSize>
const Array& operator=(const Array<T, otherSize>& rhs)
{
const T* rhsData = rhs;
std::copy(rhsData, rhsData + (size < otherSize ? size : otherSize), m_data);
return *this;
}
const T& operator[](std::size_t index) const
{
assert(index < size);
return m_data[index];
}
T& operator[](std::size_t index)
{
return const_cast<T&>( const_cast<const Array<T, size>*>(this)->operator [](index) );
}
//operator const T*( ) const
//{
// return m_data;
//}
//operator T*( )
//{
// return m_data;
//}
private:
T m_data[size];
};
__________________
quoted from C++ Coding Standards: KISS (Keep It Simple Software): Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure. Avoid magic number: Programming isn't magic, so don't incant it. |
|
#3
|
|||
|
|||
|
Re: Implicit conversion operators
Actually it seems the easiest solution is quite the opposite... remove operator[] and let the object convert to a pointer and use built-in operator[] for access to the array.
|
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|