CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Building Interactive UIs with ASP.NET Ajax: Rebinding Client-Side Events After a Partial Page Postback
  • Speed Up Repetitive Insert, Update, and Delete Query Statements
  • Binding Data to Silverlight 4.0 Controls Using ASP.NET MVC Framework 2.0
  • ADO.NET Data Services in the .NET Framework

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    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++.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old May 29th, 2009, 08:29 PM
    Speedo Speedo is offline
    Member +
     
    Join Date: Aug 2007
    Posts: 844
    Speedo  is a jewel in the rough (300+)Speedo  is a jewel in the rough (300+)Speedo  is a jewel in the rough (300+)Speedo  is a jewel in the rough (300+)
    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))?
    Reply With Quote
      #2    
    Old May 30th, 2009, 12:19 AM
    Kheun Kheun is offline
    Elite Member
     
    Join Date: Oct 2002
    Location: Singapore
    Posts: 3,128
    Kheun is a name known to all (1000+)Kheun is a name known to all (1000+)Kheun is a name known to all (1000+)Kheun is a name known to all (1000+)Kheun is a name known to all (1000+)Kheun is a name known to all (1000+)Kheun is a name known to all (1000+)Kheun is a name known to all (1000+)
    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.
    Reply With Quote
      #3    
    Old May 30th, 2009, 07:38 AM
    Speedo Speedo is offline
    Member +
     
    Join Date: Aug 2007
    Posts: 844
    Speedo  is a jewel in the rough (300+)Speedo  is a jewel in the rough (300+)Speedo  is a jewel in the rough (300+)Speedo  is a jewel in the rough (300+)
    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.
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > C++ (Non Visual C++ Issues)


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 02:26 AM.



    Acceptable Use Policy

    Internet.com
    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.