overloading operator()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mohammad

    overloading operator()

    I'm implementing a c++ template class CScan to minipulate a series of
    numbers.
    I have implemented operator() to select a range of numbers it works
    fine for an expression like:
    scan1 = scan2(0, 99);

    However, I have problem when I try something like:

    scan1(0, 99) = scan2(100, 199);

    In this case, a temporary object is created and destoryed without
    copying the values to scan1. How can I accomplish this? Following is
    some of the relavent code.


    template<class T>
    class CScan
    {
    public:
    CScan() : m_nSize(0), m_pData(NULL)
    {
    }

    CScan(ULONG nSize) : m_nSize(nSize)
    {
    m_pData = new T[m_nSize];
    ZeroMemory(m_pD ata, m_nSize * sizeof(T));
    }

    CScan(T* pData, ULONG nSize) : m_nSize(nSize)
    {
    m_pData = new T[m_nSize];
    CopyMemory(m_pD ata, pData, m_nSize * sizeof(T));
    }

    CScan(CScan& scan) : m_nSize(scan.m_ nSize)
    {
    m_pData = new T[m_nSize];
    CopyMemory(m_pD ata, scan.m_pData, m_nSize * sizeof(T));
    }

    ~CScan()
    {
    delete [] m_pData;
    }

    CScan& operator=(CScan & scan)
    {
    if (&scan == this)
    {
    return *this;
    }
    m_nSize = scan.m_nSize;
    delete [] m_pData;
    m_pData = new T[m_nSize];
    CopyMemory(m_pD ata, scan.m_pData, m_nSize * sizeof(T));
    return *this;
    }

    ULONG Size()
    {
    return m_nSize;
    }

    T& operator[](const ULONG nIndex)
    {
    if (nIndex >= m_nSize)
    {
    throw CScanError::Out sideBounds(m_nS ize, nIndex);
    }
    return m_pData[nIndex];
    }

    const T& operator[](const ULONG nIndex) const
    {
    if (nIndex >= m_nSize)
    {
    throw CScanError::Out sideBounds(m_nS ize, nIndex);
    }
    return m_pData[nIndex];
    }

    CScan operator()(cons t ULONG nFrom, const ULONG nTo) const
    {
    if (nFrom >= m_nSize)
    {
    throw CScanError::Out sideBounds(m_nS ize, nFrom);
    }
    if (nTo >= m_nSize)
    {
    throw CScanError::Out sideBounds(m_nS ize, nTo);
    }
    ULONG nSize = nTo - nFrom + 1;

    if (nSize > 0)
    {
    return CScan(&m_pData[nFrom], nSize);
    }
    else
    {
    return CScan;
    }
    }

    private:
    T* m_pData;
    ULONG m_nSize;
    };
  • Ron Natalie

    #2
    Re: overloading operator()

    Mohammad wrote:[color=blue]
    > I'm implementing a c++ template class CScan to minipulate a series of
    > numbers.
    > I have implemented operator() to select a range of numbers it works
    > fine for an expression like:
    > scan1 = scan2(0, 99);
    >
    > However, I have problem when I try something like:
    >
    > scan1(0, 99) = scan2(100, 199);
    >
    > In this case, a temporary object is created and destoryed without
    > copying the values to scan1. How can I accomplish this? Following is
    > some of the relavent code.
    >[/color]

    The trick is obviously not to use a temporary copy. The trick would to
    have Scan::operator( ) not return a Scan directly, but an helper class that
    would have a pointer/reference to the object that operator() was invoked
    on and forward the copy/assignemnts to it (possibly remapping the indices).


    Comment

    • Mohammad

      #3
      Re: overloading operator()

      "Ron Natalie" <ron@sensor.com > wrote in message news:<3f5e213a$ 0$46550$9a6e19e a@news.newshost ing.com>...[color=blue]
      > Mohammad wrote:[color=green]
      > > I'm implementing a c++ template class CScan to minipulate a series of
      > > numbers.
      > > I have implemented operator() to select a range of numbers it works
      > > fine for an expression like:
      > > scan1 = scan2(0, 99);
      > >
      > > However, I have problem when I try something like:
      > >
      > > scan1(0, 99) = scan2(100, 199);
      > >
      > > In this case, a temporary object is created and destoryed without
      > > copying the values to scan1. How can I accomplish this? Following is
      > > some of the relavent code.
      > >[/color]
      >
      > The trick is obviously not to use a temporary copy. The trick would to
      > have Scan::operator( ) not return a Scan directly, but an helper class that
      > would have a pointer/reference to the object that operator() was invoked
      > on and forward the copy/assignemnts to it (possibly remapping the indices).[/color]

      I tried this approach but having compiling problems. It seems that
      VC++ 6.0 does not support return value overloading. Are there any
      other ways? It is at all possible to do something like that in C++.

      Comment

      • Ron Natalie

        #4
        Re: overloading operator()


        "Mohammad" <mjamil314@hotm ail.com> wrote in message news:3d4a6581.0 309100632.79aa7 b63@posting.goo gle.com...[color=blue]
        > "Ron Natalie" <ron@sensor.com > wrote in message news:<3f5e213a$ 0$46550$9a6e19e a@news.newshost ing.com>...[/color]
        [color=blue]
        > I tried this approach but having compiling problems. It seems that
        > VC++ 6.0 does not support return value overloading. Are there any
        > other ways? It is at all possible to do something like that in C++.[/color]

        I dont' know that means. There's no such thing as return value overloading,
        nor is one necessary to do what I suggested.



        Comment

        • Mohammad

          #5
          Re: overloading operator()

          "Ron Natalie" <ron@sensor.com > wrote in message news:<3f5f42d3$ 0$51790$9a6e19e a@news.newshost ing.com>...[color=blue]
          > "Mohammad" <mjamil314@hotm ail.com> wrote in message news:3d4a6581.0 309100632.79aa7 b63@posting.goo gle.com...[color=green]
          > > "Ron Natalie" <ron@sensor.com > wrote in message news:<3f5e213a$ 0$46550$9a6e19e a@news.newshost ing.com>...[/color]
          >[color=green]
          > > I tried this approach but having compiling problems. It seems that
          > > VC++ 6.0 does not support return value overloading. Are there any
          > > other ways? It is at all possible to do something like that in C++.[/color]
          >
          > I dont' know that means. There's no such thing as return value overloading,
          > nor is one necessary to do what I suggested.[/color]

          Sincere thaks for your help! My confusion was partly due to some
          problem with VC++ 6.0. My code compiled and work perfectly when I used
          VC++ 7.0. Here are the relavent changes.

          //Nested class in CScan

          template<typena me T>
          class CCopyScan
          {
          friend CScan<T>;
          public:
          CCopyScan(T* pData, ULONG nSize) :
          m_pData(pData), m_nSize(nSize)
          {
          }

          CCopyScan() :
          m_pData(NULL), m_nSize(0)
          {
          }

          CCopyScan(CCopy Scan& copyscan) :
          m_pData(copysca n.m_pData), m_nSize(copysca n.m_nSize)
          {
          }

          ~CCopyScan()
          {
          }

          CCopyScan& operator=(CCopy Scan& copyscan)
          {
          m_nSize = copyscan.m_nSiz e;
          CopyMemory(m_pD ata, copyscan.m_pDat a, m_nSize * sizeof(T));
          return *this;
          }

          private:
          T* m_pData;
          ULONG m_nSize;
          };


          //Overload assignment
          CScan& operator=(const CCopyScan<T>& copyscan)
          {
          delete [] m_pData;
          m_nSize = copyscan.m_nSiz e;
          m_pData = new T[m_nSize];
          CopyMemory(m_pD ata, copyscan.m_pDat a, m_nSize * sizeof(T));
          return *this;
          }

          //New operator()
          CCopyScan<T> operator()(cons t unsigned long nFrom, const unsigned
          long nTo)
          {
          if (nFrom >= m_nSize)
          {
          throw;
          }
          if (nTo >= m_nSize)
          {
          throw;
          }
          unsigned long nSize = nTo - nFrom + 1L;
          if (nSize > 0)
          {
          return CCopyScan<T>(&m _pData[nFrom], nSize);
          }
          else
          {
          return CCopyScan<T>();
          }
          }

          Comment

          Working...