Re: Member call removes display of text????
Robby wrote:
[snip]
class Cat
{
public:
Cat(int initialAge); //Constructor
~Cat(); //Destructor
int getAge();
void setAge(int age);
void Meow(HDC hdc,RECT rect);
private:
int itsAge;
};
[snip]
Hi Robby:
You seem to have solved your problem. Just a couple of details now that
you are moving into C++. There is a very important thing called
const-correctness. This takes two forms
(1) Pointer or reference function arguments whose target is not changed
by the function should be labelled const. For example
void Display(const char* name)
{
printf("%s\n", name);
}
(2) Member functions (methods) of a class that do not change the
contents of the class should be labelled as const. There are two of
these in your example.
class Cat
{
public:
Cat(int initialAge); //Constructor
~Cat(); //Destructor
int getAge() const;
void setAge(int age);
void Meow(HDC hdc,RECT rect) const;
private:
int itsAge;
};
getAge() is a classic const method. Meow() might not be, but yours is.
If the cat aged by one year every time it meowed, then it would not be.
Now you can do
const Cat Frisky(11);
Because Frisky is a const object, you can call the getAge() and Meow()
methods on it, but not the setAge() method. If you had not labelled
getAge() and Meow() as const methods, you would not be able to use them.
It's important to code const-correctly from the beginning, because it's
a big pain to retrofit const correctness to a big program. Const
correctness is a very nice feature of the language, sadly missing in .NET.
David Wilkinson
=======================