CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2007
    Posts
    858

    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];
    };
    The problem I'm running into is if you try to make multi-dimensional arrays and access them with operator[]. Eg:

    Code:
    Array<Array<int, 5>, 5> a;
    a[0][0] = 1;
    The second line generates an error because the compiler can't decide whether to call Array: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. #2
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    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. #3
    Join Date
    Aug 2007
    Posts
    858

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured