lhs上的C++模板和函数
C++ templates and functions on lhs
给定以下类:
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.从bool
到void
的更改,这次在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
]
相关文章:
- 为什么除非添加括号,否则构造函数上的模板替换会失败?
- 不命名构造函数和析构函数上的类型错误
- 默认构造函数上的分段错误
- 运算符重载函数上的函数重载
- CPPCHECK C++成员函数上的格式字符串
- 为什么Main函数上的Sleep()会停止所有线程
- g++ 6.3,avx 内联函数上的 Kahan 求和用易失性关键字进行序列化
- 非模板化函数上的约束表达式有什么意义?
- 尝试"复制"shared_ptr向上转换行为会导致复制构造函数上的无限递归(导致段错误)
- 函数上的关键字'const'意味着对象字段完全只读?
- 模板类内 lambda 函数上的 __declspec(noinline) 在 VS2017 中抛出语法错误
- 如何在C++中实现函数上的二叉搜索?
- 此模板函数上的最后一个参数有什么用?
- 替代基础析构函数上的虚拟函数调用
- C++复制构造函数上的双重释放错误
- 递归函数上的分割故障C
- 仅更改函数上的特定默认参数
- 好友函数上的模板链接器错误
- 非调用转换函数上的模板中的编译错误
- 构造函数上的SFINAE在VC2017中工作,但在clang / gcc中不起作用