🔒 Locked
overloading operators?
Advertisement
Original Post
I''m doing this exactly the same way the book has done it but i keep getting errors. Since I''m new to this I don''t know where to start debugging.
Here''s the class in which I declare the operator thing:
class CVehicle //no base classes
{
friend class CWeathersystem;
private:
unsigned int weight,capacity;
float topspeed;
public:
void printdata();
//constructors and destructors
CVehicle();
~CVehicle();
unsigned int getweight(){return weight;}
unsigned int getcapacity(){return capacity;}
float gettopspeed(){return topspeed;}
int setweight(unsigned int newweight);
int setcapacity(unsigned int newcapacity);
int settopspeed(float newtopspeed);
CVehicle operator + (CVehicle right); //<----------here
};
Here''s the function:
inline CVehicle CVehicle::operator + (CVehicle right)
{
return CVehicle(weight+right.weight,capacity+right.capacity,
topspeed+right.topspeed);
}
I dont'' see what''s wrong with this. All it does is return the added weight, topspeed and capacity of the 2 vehicle objects.
hm... what error does the compiler give you ?
Anyway, I don''t think you can specify the inline keyword outside the class declaration. Try something like this :
Anyway, I don''t think you can specify the inline keyword outside the class declaration. Try something like this :
|
You need another constructor with which to create the vehicle you are returning.
CVehicle(int weight, int capacity, float topspeed);
You probably want to rename the members so they can be distinguished from parameters - ie m_weight etc.
CVehicle (int weight, int capacity, float topspeed)
:m_weight(weight), m_capacity(capacity), m_topspeed(topspeed)
{
}
CVehicle(int weight, int capacity, float topspeed);
You probably want to rename the members so they can be distinguished from parameters - ie m_weight etc.
CVehicle (int weight, int capacity, float topspeed)
:m_weight(weight), m_capacity(capacity), m_topspeed(topspeed)
{
}
quote:
Original post by Prosper/LOADED
hm... what error does the compiler give you ?
Anyway, I don''t think you can specify the inline keyword outside the class declaration. Try something like this :
Actually you can. Functions written inside the class declaration are automatically considered "inline". If you want a member function to be inline but the code for it elsewhere it''s perfectly valid to use inline outside the class. However, there is no guarentee that functions will be expanded as inline.
-------
Andrew
It looks like you have a space between the word operator and the symbol +
You need operator+ (Cvehicle...)
Hope this helps
Brad
You need operator+ (Cvehicle...)
Hope this helps
Brad
How would you define the << operator with the ostream class, so you could do something like this:
without getting where it''s stored in memory (0xABCDEF12 or something)?
|
without getting where it''s stored in memory (0xABCDEF12 or something)?
Here''s a simple example on how to overload the << operator.
-------
Andrew
|
-------
Andrew
quote:
Original post by brad_beveridge
It looks like you have a space between the word operator and the symbol +
You need operator+ (Cvehicle...)
Sorry, not true. operator is a keyword just like any other in C++. The help for operator says the following:
quote:
from MSDNoperator
C++ Specific —>
type operator operator-symbol ( parameter-list )
The operator keyword declares a function specifying what operator-symbol means when applied to instances of a class. This gives the operator more than one meaning, or "overloads" it. The compiler distinguishes between the different meanings of an operator by examining the types of its operands.
C++ (and C) ignores whitespace.
The problem is the distinction between binary and unary operators. + is both binary and unary (a + b <- binary; +b <-unary). Unary + is useless for most pseudo-arithmetic types (it doesn''t change the value: +(-3) is -3). Binary + cannot be implemented as a member function since it needs two objects and returns a third. You can make += a member function, though, since it modifies the left-hand parameter:
class CVehicle
{
...
public:
inline CVehicle &operator += (CVehicle &v);
friend CVehicle &operator + (CVehicle &v1, CVehicle &v2);
};
//
// compiler will attempt to make this inline
CVehicle &CVehicle::operator += (CVehicle &v)
{
weight += v.weight;
capacity += v.capacity;
topspeed += v.topspeed;
//
// return the modified object
// note that ''this'' is a pointer so it must be dereferenced.
return *this;
}
//
// note that opertor+ is not a member function
CVehicle &operator + (CVehicle &v1, CVehicle &v2)
{
CVehicle v; // uses default constructor; better have one!
//
// operator + can access private data member because it is a
// friend function:
v.weight = v1.weight + v2.weight;
v.capacity = v1.capacity + v2.capacity;
v.topspeed = v1.topspeed + v2.topspeed;
//
// return the result of the addition
return v;
}
You could have used a different constructor for v in operator+ above (CVehicle v(v1.weight + v2.weight, v1.capacity + v2.capacity, ...)).
Oh, and use whitespace liberally. As I''ve noted, C++ ignores it so it makes code nicer to look at and read.
To elaborate (unnecessarily
) on the binary operator thing:
If the first operand of an operator is an object, then that operator can reside in the class of that object, but if the first operand is a builtin-type, or a class-type that you cannot change the declaration for, then you must declare the operator as a global functions.
e.g. consider a vector class that must have the binary multiplication defined for multiplying a scalar with the vector:
(the rest of the class was omitted for brevity)
If the first operand of an operator is an object, then that operator can reside in the class of that object, but if the first operand is a builtin-type, or a class-type that you cannot change the declaration for, then you must declare the operator as a global functions.
e.g. consider a vector class that must have the binary multiplication defined for multiplying a scalar with the vector:
|
(the rest of the class was omitted for brevity)
As Gillies stated you only need a new constructor for the operator overloader function as follow:
And the code will work as it should.
But since you almost always want to initialize the values one way or the other I would remove the CVehicle() constructor and replace it with this one that use default values:
Edited by - errand on October 24, 2001 7:31:06 AM
|
And the code will work as it should.
But since you almost always want to initialize the values one way or the other I would remove the CVehicle() constructor and replace it with this one that use default values:
|
Edited by - errand on October 24, 2001 7:31:06 AM
quote:
Original post by Anonymous Poster
you can make the binary + a member function, just only have one parameter.
I looked that up, and he''s right. Thanks!
quote:
However it is considered better form to have it be a non member function and implement it in terms of +=. That way it lets implicit constuctors work on the left operand
I think it''s a bad idea to implement operator+ in terms of operator+=, because a + normally takes its lhs and rhs operands, adds them together and returns the sum without modifying either of them. If you''re speaking of using operator+= on the sum object, well isn''t that inefficient? Consider:
// using += on one of the operands:
myclass &operator+(myclass &m1, myclass &m2)
{
m1 += m2;
return m1;
}
// this has the effect of making c = a + b set c to the sum, but
// a to the sum as well.
//
// using += on the sum object:
myclass &operator+(myclass &m1, myclass &m2)
{
myclass m;
m += m1;
m += m2;
return m;
}
// this is just plain stoopid.
//
// what I consider to be the best way for pseudo-arithmetic types:
myclass &operator+(myclass &m1, myclass &m2)
{
mylcass m;
m = m1 + m2; // this would actually be a + on the individual fields,
// such as the coordinates in a vector
return m;
}
// c = a + b behaves properly.
Alternatively, as a member function:
myclass &myclass::operator+(myclass &m)
{
myclass r = *this;
r += m;
return r;
// 2 assignments, an extra copy of r and one addition.
// compare to one copy of the sum, one addition and one assignment for friend version
}
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement