销毁和创建一个对象 [英] Destroying and creating an object
问题描述
最近,当我转到
路障时,我已经提出了一套很好的矩阵代码。我想知道我是否应该销毁一个物体
用新的dimmensions创建一个新物体。
class Matrix {
public:
Matrix(int = 4,int = 4);
~Matrix();
Matrix& setDims(int, int);
Matrix& setRows(int);
Matrix& setCols(int);
Matrix& setVal(int, int,float);
Matrix& setInitVals();
Matrix& setIdentity();
Matrix& preMultiply(Matrix& );
int getRows()const;
int getCols()const;
float getVal(int,int)const;
const Matrix& operator =(const Matrix&);
private:
int rows,cols;
float * * m;
};
Matrix :: Matrix(int rows,int cols){
setDims(rows,cols) );
m = new float * [getRows()];
for(int i = 0; i< getRows(); i ++){
m [i] = new float [getCols()];
};
se tInitVals(); //这一步是必要的
};
Matrix :: ~Matrix(){
for(int i = getRows () - 1; i> -1; i - ){
delete [] m [i];
};
删除[] m;
};
Matrix& Matrix :: setDims(int rows,int
cols){ setRows(rows); setCols(cols); return * this;};
Matrix& Matrix :: setRows(int rows){this-> rows = rows; return * this;};
Matrix& Matrix :: setCols(int cols){this-> cols = cols; return * this;};
Matrix& Matrix :: setVal( int row,int col,float val){m [row] [col] =
val;};
int Matrix :: getRows()const {return rows; };
int Matrix :: getCols()const {return cols;};
float Matrix :: getVal(int row,int col)const {return m [row ] [col];};
Matrix& Matrix :: setIdentity(){
for(int i = 0; i< this-> ; getRows(); i ++){
for(int j = 0; j< this-> getCols(); j ++){
this-> setVal (I,J,S tatic_cast< float>(i == j));
};
};
返回* this;
};
Matrix& Matrix :: preMultiply(Matrix& n){
// m [i] [j] = m [ i] [k] * n [k] [j]
尝试{
if(this-> getCols()!= n.getRows())throw 1 ;
矩阵t(this-> getRows(),n.getCols());
for(int i = 0;我< t.getRows(); i ++){
for(int j = 0; j< t.getCols(); j ++){
t.setVal(i,j,0.0);
for(int k = 0; k< t.getRows(); k ++){
t.setVal(i,j,t.getVal(i,j) + this-> getVal(i,k)* n.getVal(k,j));
};
};
};
if(t.getRows()!= n.getRows())throw 2;
/ * =========兴趣区========== * /
n = t; //我是否应该销毁对象n以便使用t'的dimmensions取代它?
}
catch( int e){
cout<< 例外类型 << e<< "发生"。 <<结束;
开关(e){
案例1:
案例2:
cout<< 不足的基质dimmesions。 <<结束;
休息;
默认值:
cout<< 未知异常类型 << e<< "发生"。 <<结束;
};
}
catch(...){
cout<< 发生未知异常。 <<结束;
};
};
const矩阵& Matrix :: operator =(const Matrix& rhs){
for(int i = 0; i< rhs.getRows(); i ++){
for(int j = 0; j< rhs.getCols(); j ++) {
this-> setVal(i,j,rhs.getVal(i,j));
};
};
返回* this;
};
Bushido Hacks写道:我最近遇到了一套很好的矩阵代码,当我转向
路障时。我想知道我是否应该销毁一个物体以创建一个具有新的维度的新物体。
[...]
Matrix& Matrix :: preMultiply(Matrix) & n){
// m [i] [j] = m [i] [k] * n [k] [j]
尝试{
if(this-> getCols()!= n.getRows())throw 1;
这是对例外情况的明确滥用。投掷和捕捉相同的
功能?请...你不能只写''if - else''?
Matrix t(this-> getRows(),n.getCols());
for(int i = 0; i< t.getRows(); i ++){
for(int j = 0; j< t.getCols(); j ++){
t.setVal( i,j,0.0);
for(int k = 0; k< t.getRows(); k ++){
t.setVal(i,j,t.getVal(i,j) + this-> getVal(i,k)* n.getVal(k,j));
};
};
};
if(t.getRows( )!= n.getRows())扔2;
/ * =========利益区========== * /
n = t; //我是否应该销毁对象n,以便使用t'的dimmensions取代它?
//新n?
为什么要销毁''n''?难道不是'这个'你应该改变的对象吗?
什么''preMultiply''做,改变它的论点?这是糟糕的设计,IMO。
}
catch(int e){
[...]
V
" Bushido Hacks" < BU ********** @ gmail.com>在消息中写道
news:11 ********************** @ z14g2000cwz.googlegr oups.com ...最近,当我转向一个
路障时,我想出了一套很好的矩阵代码。我想知道是否应该销毁一个物体以创建一个具有新调光的新物体。
Matrix& Matrix :: preMultiply(Matrix& n){
// [[[[[[[[[[[[[[[[[[[[[[[ ))抛出1;
矩阵t(this-> getRows(),n.getCols());
for(int i = 0; i< t.getRows(); i ++){
for(int j = 0; j< t.getCols(); j ++){
t.setVal(i,j,0.0);
for(int k = 0; k < t.getRows(); k ++){
t.setVal(i,j,t.getVal(i,j)+ this-> getVal(i,k)* n.getVal(k,j );
};
};
};
if(t.getRows()!= n.getRows())throw 2;
/ * = ========兴趣领域========== * /
n = t; //我应该销毁对象n,以便
//带有t'的dimmensions的新n替换它吗?
}
参数n是对现有对象的引用。你如何建议
你会破坏它并创建一个新的?
一个解决方案可能是修改operator =以便它删除它的
内部数组,重新分配它们,然后复制数据。
但我很困惑这个函数是如何使用的,以及它返回的是什么。
你为什么要改变n,但是返回*这个,这个没有改变?你是否确实想要返回t,而是单独留下?在这种情况下,由于t
是一个局部变量,你不想返回一个引用,但是要么是一个
指针(并使用new来创建t,依靠别处的代码来删除它)或
a副本(在这种情况下你需要一个有效的复制构造函数。
-Howard
>
啊,但*这也有不同于t的参数。
我个人更喜欢这种异常处理方法有两个原因: br />
没有太多人理解如何使用异常处理,他们会使用断言而不是
.Assert需要一个额外的包含文件,其中
try / throw / catch是C ++。
如果/ else不适合异常处理。抛出异常是
更好。
I''ve come up with a good set of Matrix codes recently when I cam to a
road block. I would like to know if I should destroy an object to
create a new object with new dimmensions.
class Matrix{
public:
Matrix(int = 4, int = 4);
~Matrix();
Matrix &setDims(int, int);
Matrix &setRows(int);
Matrix &setCols(int);
Matrix &setVal(int,int,float);
Matrix &setInitVals();
Matrix &setIdentity();
Matrix &preMultiply(Matrix&);
int getRows() const;
int getCols() const;
float getVal(int,int) const;
const Matrix &operator=(const Matrix&);
private:
int rows, cols;
float** m;
};
Matrix::Matrix(int rows, int cols){
setDims(rows,cols);
m = new float*[getRows()];
for(int i = 0; i < getRows(); i++){
m[i] = new float[getCols()];
};
setInitVals(); // this step is neccessary
};
Matrix::~Matrix(){
for(int i = getRows() - 1; i > -1; i--){
delete[] m[i];
};
delete[] m;
};
Matrix &Matrix::setDims(int rows,int
cols){setRows(rows);setCols(cols);return *this;};
Matrix &Matrix::setRows(int rows){this->rows = rows;return *this;};
Matrix &Matrix::setCols(int cols){this->cols = cols;return *this;};
Matrix &Matrix::setVal(int row, int col, float val){m[row][col] =
val;};
int Matrix::getRows() const {return rows;};
int Matrix::getCols() const {return cols;};
float Matrix::getVal(int row,int col) const {return m[row][col];};
Matrix &Matrix::setIdentity(){
for(int i = 0; i < this->getRows();i++){
for(int j = 0; j < this->getCols();j++){
this->setVal(i,j,static_cast<float>(i == j));
};
};
return *this;
};
Matrix &Matrix::preMultiply(Matrix &n){
// m[i][j] = m[i][k] * n[k][j]
try{
if(this->getCols() != n.getRows()) throw 1;
Matrix t(this->getRows(),n.getCols());
for(int i = 0; i < t.getRows(); i++){
for(int j = 0; j < t.getCols(); j++){
t.setVal(i,j,0.0);
for(int k = 0; k < t.getRows();k++){
t.setVal(i,j,t.getVal(i,j) + this->getVal(i,k) * n.getVal(k,j));
};
};
};
if(t.getRows() != n.getRows()) throw 2;
/* ========= AREA OF INTEREST ========== */
n = t; // Should I destroy object n so that a
// new n with t''s dimmensions replaces it?
}
catch(int e){
cout << "Exception type " << e << " occured." << endl;
switch(e){
case 1:
case 2:
cout << "Insufficent matrix dimmesions." << endl;
break;
default:
cout << "Unknown exception type " << e << " occured." << endl;
};
}
catch(...){
cout << "Unknown exception occured." << endl;
};
};
const Matrix &Matrix::operator=(const Matrix &rhs){
for(int i = 0; i < rhs.getRows();i++){
for(int j = 0; j < rhs.getCols();j++){
this->setVal(i,j,rhs.getVal(i,j));
};
};
return *this;
};
Bushido Hacks wrote:I''ve come up with a good set of Matrix codes recently when I cam to a
road block. I would like to know if I should destroy an object to
create a new object with new dimmensions.
[...]
Matrix &Matrix::preMultiply(Matrix &n){
// m[i][j] = m[i][k] * n[k][j]
try{
if(this->getCols() != n.getRows()) throw 1;
This is a definite abuse of exceptions. Throwing and catching in the same
function? Please... Couldn''t you just write ''if -- else''?
Matrix t(this->getRows(),n.getCols());
for(int i = 0; i < t.getRows(); i++){
for(int j = 0; j < t.getCols(); j++){
t.setVal(i,j,0.0);
for(int k = 0; k < t.getRows();k++){
t.setVal(i,j,t.getVal(i,j) + this->getVal(i,k) * n.getVal(k,j));
};
};
};
if(t.getRows() != n.getRows()) throw 2;
/* ========= AREA OF INTEREST ========== */
n = t; // Should I destroy object n so that a
// new n with t''s dimmensions replaces it?
Why destroy ''n''? Isn''t ''*this'' the object that you should be changing?
What''s ''preMultiply'' do, changes its argument? That''s bad design, IMO.
}
catch(int e){
[...]
V
"Bushido Hacks" <bu**********@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...I''ve come up with a good set of Matrix codes recently when I cam to a
road block. I would like to know if I should destroy an object to
create a new object with new dimmensions.
Matrix &Matrix::preMultiply(Matrix &n){
// m[i][j] = m[i][k] * n[k][j]
try{
if(this->getCols() != n.getRows()) throw 1;
Matrix t(this->getRows(),n.getCols());
for(int i = 0; i < t.getRows(); i++){
for(int j = 0; j < t.getCols(); j++){
t.setVal(i,j,0.0);
for(int k = 0; k < t.getRows();k++){
t.setVal(i,j,t.getVal(i,j) + this->getVal(i,k) * n.getVal(k,j));
};
};
};
if(t.getRows() != n.getRows()) throw 2;
/* ========= AREA OF INTEREST ========== */
n = t; // Should I destroy object n so that a
// new n with t''s dimmensions replaces it?
}
The parameter n is a reference to an existing object. How do you propose
you would destroy it and create a new one?
One solution might be to modify the operator= so that it deletes its
internal arrays, re-allocates them, then copies the data.
But I''m confused as to how this function is used, and what it is returning.
Why do you want to alter n, but return *this, which is not altered? Do you
really want to return t instead, and leave n alone? In that case, since t
is a local variable, you don''t want to return a reference, but either a
pointer (and use new to create t, relying on code elsewhere to delete it) or
a copy (in which case you need a valid copy constructor.
-Howard
Ah, but *this also has different parameters than t.
I personally, prefer this method of exception handling for two reasons:
Not too many people understand how to use exception handling, they''d
rather use assert. Assert requires an extra include file where as
try/throw/catch are of C++.
If/else is not ideal for exception handling. Throwing exceptions is
better.
这篇关于销毁和创建一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!