lhs上的C++模板和函数

C++ templates and functions on lhs

本文关键字:函数 上的 C++ lhs      更新时间:2023-10-16

给定以下类:

template< class T> class table2D
{
   ...
  public:
   bool  copy(int r, int c, int rows , int cols, table2D & s ) ;
};

其中,方法copy()将块或元素从s复制到this,

我如何对上面的代码进行编码(使用模板??),以便我可以使用如下方法:

table2D  s, d ;
d.copy(0, 0, 3, 3) = s ;

我不同意tadman的评论,所以我粗略地编写了代码(可能有一些错误,但我认为概念是正确的)。

我相信有更优雅和通用的方法来编码这个。但我认为这类事情在很大程度上是C++的工作方式。

创建占位符对象是一种非常标准的C++技术,用于保留函数的输入,该函数的真正工作需要稍后完成。

template< class T> class copier4i
{
    copier4i(T& t, int a, int b, int c, int d) : m_t(t), m_a(a), m_b(b), m_c(c), m_d(d) {}
    bool operator=(T& s) { return m_t.copy(m_a,m_b,m_c,m_d,s); }
    T& m_t;
    int m_a, m_b, m_c, m_d;
};
...
template< class T> class table2D
{
   ...
  public:
   bool  copy(int r, int c, int rows , int cols, table2D & s ) ;
   copier4i<table2D>  copy(int r, int c, int rows , int cols) {
      return copier4i<table2D>(*this,r,c,rows,cols); }
};

我没有一个很好的地方来测试这一点,所以我不确定我刚才添加的额外的<table2D>是否是正确的解决方案(对于我所期望的推断),或者问题是否是(我从未完全清楚的)table2D在其自己的定义中使用的名称,什么时候它隐含地意味着table2D<T>(正如我在这里想要的那样),什么时候它需要显式的(就像在table2D的定义之外使用一样)。所以也许它应该是:

   auto  copy(int r, int c, int rows , int cols) {
      return copier4i(*this,r,c,rows,cols); }

或者

   copier4i<table2D<T> >  copy(int r, int c, int rows , int cols) {
      return copier4i<table2D<T> >(*this,r,c,rows,cols); }

新版本,包括George T.从boolvoid的更改,这次在ideone(作为C++14代码)上进行了测试

#include <stdio.h>
#include <stdlib.h>

template< class T > class copier4i
{
  public:
     T&  m_t;
     int m_a, m_b, m_c, m_d;
     copier4i(T& t, int a, int b, int c, int d) : m_t(t), m_a(a), m_b(b), m_c(c), m_d(d) {}
     copier4i& operator=(T& s) { m_t.copy(m_a,m_b,m_c,m_d,s); return *this;}
} ;
template< class T> class table2D
{
   public:
     int m_rows  ;
     int m_cols  ;
     T  * m_obj  ;
   public:
     table2D( int r, int c ) {
        m_rows = r ;
        m_cols = c ;
        m_obj  = ( T * )malloc( m_rows * m_cols * sizeof( T ) ) ;
     }
     ~table2D() {
        free( m_obj );
     }
     inline T *    operator[](int  r) {
        return  (this->m_obj + (r * m_cols ) ) ;
     }
     void  copy( int r, int c, int cr, int cc, table2D &s) {
         (*this)[r][c] = s[0][0]  ;
     }
     copier4i< table2D >  copy(int r, int c, int rows , int cols)
     {
        return copier4i<table2D>( *this, r, c, rows, cols );
     }
} ;

int main()
{
  table2D<int> t(5, 5);
  table2D<int> s(5, 5);
  s.copy(0, 0, 2, 2, t );
  s.copy(0, 0, 2, 2) = t ;
}

感谢您的帮助。是的,它可能看起来很奇怪,但一旦你习惯了,它也有它的优点

虽然发布的代码没有编译,但我对模板的了解并没有发现错误。你能看到下面的吗:

#include <stdio.h>
#include <stdlib.h>

template< class T > class copier4i
{
  public:
     T&  m_t;
     int m_a, m_b, m_c, m_d;
     copier4i(T& t, int a, int b, int c, int d) : m_t(t), m_a(a), m_b(b), m_c(c), m_d(d) {}
     bool operator=(T& s) { return m_t.copy(m_a,m_b,m_c,m_d,s); }
} ;
template< class T> class table2D
{
   public:
     int m_rows  ;
     int m_cols  ;
     T  * m_obj  ;
   public:
     table2D( int r, int c ) {
        m_rows = r ;
        m_cols = c ;
        m_obj  = ( T * )malloc( m_rows * m_cols * sizeof( T ) ) ;
     }
     ~table2D() {
        free( m_obj );
     }
     inline T *    operator[](int  r) {
        return  (this->m_obj + (r * m_cols ) ) ;
     }
     void  copy( int r, int c, int cr, int cc, table2D &s) {
         (*this)[r][c] = s[0][0]  ;
     }
     copier4i< table2D >  copy(int r, int c, int rows , int cols)
     {
        return copier4i( *this, r, c, rows, cols );
     }
} ;

void main()
{
  table2D<int> t(5, 5);
  table2D<int> s(5, 5);
  s.copy(0, 0, 2, 2, t );
  s.copy(0, 0, 2, 2) = t ;
}

错误为:

ttt.cpp
ttt.cpp(40) : error C2955: 'copier4i' : use of class template requires template argument list
        ttt.cpp(6) : see declaration of 'copier4i'
        ttt.cpp(39) : while compiling class template member function 'copier4i<T> table2D<int>::copy(int,int,int,int)'
        with
        [
            T=table2D<int>
        ]
        ttt.cpp(48) : see reference to class template instantiation 'table2D<T>' being compiled
        with
        [
            T=int
        ]