In classes, my book uses examples like
Things like that.Code:GetAge(int itsage) const {return itsage} /*and*/ SetAge(int age) {itsage = int age}
Why would you do this and not jsut combine the functions?
In classes, my book uses examples like
Things like that.Code:GetAge(int itsage) const {return itsage} /*and*/ SetAge(int age) {itsage = int age}
Why would you do this and not jsut combine the functions?
>> GetAge(int itsage) const {return itsage}
That should be:
where it returns the member variable.Code:int GetAge() const {return itsage;}
>> Why would you do this and not jsut combine the functions?
How would you combine the functions? How would you be able to tell the difference between codethat wanted to get the current value and code that wanted to change the current value?
Ah good point I think it was just that the book always used them in a row so that there was really no point in having the two.
as a general design point, don't provide getter and setter function for private variables unless you really need it. As soon as you do that, you're essentially exposing them as part of the interface of the class.
for example, in a game you might have a Player class, with a private int member representing player health. Instead of a SetHealth method, it's better to have Damage/Heal/Kill/whatever methods. That way if you decide that health should be a float (for some reason), you don't break the interface
"I saw a sign that said 'Drink Canada Dry', so I started"
-- Brendan Behan
Free Compiler: Visual C++ 2005 Express
If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?
another common idiom:
Code:struct person { int age(void) const { return age_; } void age(int set) { age_ = set; } protected: int age_; };
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }