Sorry about this dumb question, what is "this" pointer... i've read through it but can you put it in the context of my classes cause it's not 100% clear to me
Printable View
Nothing wrong with my write function, think error is related to code above it!Code:
Contract::Contract(): issued( Date::getCurrentTime() );// is this semicolon necessary?
// Display the customer's contract, or save it to text file
void Contract::Write(ostream& out, bool isScreen)
{
}
// : error C2062: type 'void' unexpected
// : error C2630: ';' found in what should be a comma-separated list
// : error C2065: 'isScreen' : undeclared identifier
// : error C2065: 'out' : undeclared identifier
// : warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
You should havein your Contract constructor, instead of the semicolon. Even if you don't do anything in the constructor, it does need a (empty) function bodyCode:{}
--
Mats
It is "this" object. e.g.
Most of the time, you don't need the this pointer, but it is passed to all non-static member functions, including constructor.Code:class X
{
public:
X();
void foo();
private:
int x;
}
void X::foo()
{
x = 7;
// the above is short for
this->x = 7;
}
--
Mats
I agree with this. An object that has "nonsense" in it because the constructor placed it there is pretty meaningless.
On a different not, more towards the contract of rentals: You may want to actually fill in the date of expected return, even if that's not the actual return date. You can then correct it when the car is actually returned.
--
Mats
Anything wrong with this overloading
ts is enum type....Code:Car::TankStatus operator=(int number, Car::TankStatus ts)
{
return ts = number;
}
//: error C2801: 'operator =' must be a non-static member
I believe the left-hand side of operator= must be a user-defined type (you have int) because it can only be overloaded as a non-static member.
The bold part is what would be on the left of the assignment operator.Code:Car::TankStatus operator=(int number, Car::TankStatus ts)
But anyway, it seems that you can't overload operator= for enum types anyway since it can only be overloaded as non-static member function.
Come to think of it, how would you implement the assignment of the actual enum value, if operator= is not available (since you are in the process of defining it)?
You can already assign enum to int and to enum, and you can assign int to enum with the help of a cast (but be careful with that since the int may not contain one of the enumerated values).
Operator = must be a member of the class to which it is defined to work with.
As thus, operator = does not work for enums, only classes.
Either way, you should not assign an int to an enum; you're essentially circumventing the whole C++ type safety.
You can certainly make "global" operator=() operator, that takes two arbitrary classes [that you have some idea of how you would convert to the left-hand side type].
The reason, I believe, for this operator is the need to serialize the data to file. If it was stored from the enum on the file, then it should be safe to read back. We could add checks in the operator to ensure that's the case.
--
Mats
But how?Quote:
You can certainly make "global" operator=() operator, that takes two arbitrary classes [that you have some idea of how you would convert to the left-hand side type].
Code:struct X {};
struct Y {};
X& operator= (X& x, const Y&) //X& operator=(X&, const Y&)' must be a nonstatic member function
{
return x;
}
Searching the web, I can't actually find it, nor in Stroustrup (but the index is a bit hard to use in Stroustrup - doesn't always list what you want).
--
Mats