Passing Classes as Parameters

Started by
2 comments, last by 31337 20 years, 8 months ago
I''ve ran into problems with my vector class. Mainly, with operators. My problem is that my operators all look kindof like this: class CVector { public: inline CVector operator + (CVector &vec) { CVector returnVal; returnVal.x = x + vec.x; // and so on return returnVal; } float x, y, z; }; The problem with this approach is that when it runs into my = operator, which also takes a CVector &, it doesn''t have a & to a CVector but rather the CVector itself. Also, when I change the return type of the CVector class to a reference, I get bogus results. Ultimately, I need to get something like this to work: CVector newVec = oldVec + olderVec; Thanks in advance.
OpenGL Revolutions http://students.hightechhigh.org/~jjensen/
Advertisement
quote:The problem with this approach is that when it runs into my = operator, which also takes a CVector &, it doesn''t have a & to a CVector but rather the CVector itself.

Isn''t a problem, this is the way you do it. You''re passing a reference (usually const) of the object, and then assign individual member variables of this passed object into the current object. You then return a reference to the current object so you can chain operations.

However, with classes that don''t contain any pointers, just regular variables, you don''t need to overload the assignment operator (or even the copy constructor, not sure) since the compiler can easily do a shallow copy.

When you say you get bogus results, exactly what do you mean? And of what operator do you change the return type to a reference. Any code to show?
quote:Original post by Zipster
quote:The problem with this approach is that when it runs into my = operator, which also takes a CVector &, it doesn''t have a & to a CVector but rather the CVector itself.

Isn''t a problem, this is the way you do it. You''re passing a reference (usually const) of the object, and then assign individual member variables of this passed object into the current object. You then return a reference to the current object so you can chain operations.

However, with classes that don''t contain any pointers, just regular variables, you don''t need to overload the assignment operator (or even the copy constructor, not sure) since the compiler can easily do a shallow copy.

When you say you get bogus results, exactly what do you mean? And of what operator do you change the return type to a reference. Any code to show?


Indeed...

Usually classes like this I write something like this:

class CVector{public:  CVector( void );  CVector( float x , float y , float z );  CVector( CVector & );  CVector( const CVector & );  CVector operator+(CVector vec); //<-- or one for both CVector & and const CVector &.  CVector &operator=(CVector vec); //<-- again, this could be replaced with two funcs, for reference and const reference.  ...etc...}; 


Allways seems to work for me.
yes what Zipster said

For your assignment operator it should look something like this:

CVector & operator= (constCVector &vec); 


And you don''t need to put inline for your operator+. Since you defined it right in the class declaration it''s automatically inlined. And you wouldn''t want to return a reference in your operator+ because you''ll be referencing a temporary variable.




--{You fight like a dairy farmer!}

--{You fight like a dairy farmer!}

This topic is closed to new replies.

Advertisement