CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2003
    Location
    Morelia, Mexico
    Posts
    40

    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:
    0
    1
    2
    3

    -33686019 0 0 0
    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);}

  2. #2
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968

    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.
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  3. #3
    Join Date
    Apr 2003
    Location
    Morelia, Mexico
    Posts
    40

    Re: returning objects, not all members are copied?

    Quote 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);}

  4. #4
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968

    Re: returning objects, not all members are copied?

    Quote 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<TGoperator*(const CMatriz<TG>& left_,const CMatriz<TG>& right_)
    {
      
    CMatriz<TGTmp;
      
    //Code here to add left and right to Tmp
        
    return Tmp;
    }

    template<class TG>
    CMatriz<TGoperator+(const CMatriz<TG>& left_,const CMatriz<TG>& right_)
    {
      
    CMatriz<TGTmp;
      
    //Code here to add left and right to Tmp
        
    return Tmp;

    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  5. #5
    Join Date
    Apr 2003
    Location
    Morelia, Mexico
    Posts
    40

    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
  •  





Click Here to Expand Forum to Full Width

Featured