|
-
September 21st, 2005, 04:45 PM
#1
returning objects, not all members are copied?
The template declaration:
Code:
template<class TG> class CMatriz
{
public:
CMatriz(unsigned int j,unsigned int k=1);
~CMatriz();
CMatriz<TG> T(void);
void inserta(unsigned int j,unsigned int k,const TG &op);
TG obten(unsigned int j,unsigned int k) const;
CMatriz<TG> operator=(const CMatriz<TG> &op);
friend ostream &operator<<(ostream &os,const CMatriz<TG> &op);
protected:
void Destruye();
void Crea(unsigned int j,unsigned int k);
unsigned int m_reng,m_cols;
vector<TG> m_matriz;
};
Problem when calling the function:
Code:
template<class TG>
CMatriz<TG> CMatriz<TG>::T(void)
{
unsigned int i,j;
CMatriz<TG> transpuesta(m_cols,m_reng);
for(i=0;i<m_reng;i++)
for(j=0;j<m_cols;j++)
transpuesta.inserta(j,i,obten(i,j));
return transpuesta;
}
Code executed:
Code:
CMatriz<int> matriz(4),matriz2(2);
matriz.inserta(0,0,0);
matriz.inserta(1,0,1);
matriz.inserta(2,0,2);
matriz.inserta(3,0,3);
cout << matriz;
cout << matriz.T();
the output is:
The output I'm expecting is "0 1 2 3" (I do get this output if I do cout << transpuesta within CMatriz<TG>::T(void) right before return transpuesta)
The object put in place of matriz.T() is supposed to be a copy of transpuesta but m_matriz is not being copied.
Note: ~CMatriz() is empty.
Any hints?
int i;main(){for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"hell\
o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}
-
September 21st, 2005, 05:07 PM
#2
Re: returning objects, not all members are copied?
You're passing CMatriz back by value, but you don't have a copy constructor.
You should add a copy constructor to class CMatriz.
Also, I don't recommend passing by value. It's not efficient for a matirx class to be pass by value.
-
September 21st, 2005, 05:20 PM
#3
Re: returning objects, not all members are copied?
 Originally Posted by Axter
You're passing CMatriz back by value, but you don't have a copy constructor.
You should add a copy constructor to class CMatriz.
Also, I don't recommend passing by value. It's not efficient for a matirx class to be pass by value.
Thanks for the quick reply.
I'm researching as to how to do a copy constructor, it's been a while since I did some basic programming in C++ and I don't remember ever coming into that.
To make it efficient how should I do it?
I want to be able to write expressions like:
A = A*B.T()+2.0;
Thanks again.
int i;main(){for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"hell\
o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}
-
September 21st, 2005, 05:33 PM
#4
Re: returning objects, not all members are copied?
 Originally Posted by Luis G
To make it efficient how should I do it?
I want to be able to write expressions like:
A = A*B.T()+2.0;
Thanks again.
If you want to use the above syntax, that does make it hard to avoid passing by value.
However, I recommend you pass by value in your operators.
Example:
PHP Code:
template<class TG> CMatriz<TG> operator*(const CMatriz<TG>& left_,const CMatriz<TG>& right_) { CMatriz<TG> Tmp; //Code here to add left and right to Tmp return Tmp; }
template<class TG> CMatriz<TG> operator+(const CMatriz<TG>& left_,const CMatriz<TG>& right_) { CMatriz<TG> Tmp; //Code here to add left and right to Tmp return Tmp; }
-
September 21st, 2005, 05:43 PM
#5
Re: returning objects, not all members are copied?
Too bad there's no way (or easy way) to keep the natural syntax I want and make it efficient.
I was thinking I should implement those operators just as you suggested.
Thanks again for the help, it works properly now
int i;main(){for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"hell\
o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|