Re: About pure virtual function
Lorry Astra wrote:
class Pet
{
public:
virtual void speak() const = 0;
virtual void eat() const = 0;
};
class Dog : public Pet {
public:
void eat() const {}
};
int main()
{
// Dog g;
}
If I define an object of class Dog and compile it, it must be an error,
'cos I don't define another one in class Dog, but I consider that whether
the compiler should tell me what is wrong when I don't define object "d".
I guess that you mean that it must be an error because you didn't define
speak(), which is true. I have no idea which object "d" you mean...
Because I always feel that the current state of class Dog is like a trap,
and it doesn't have any meaning . Am i right?
Since not all pure virtual functions have been implemented, class Dog also
has pure virtual functions by inheritance, so it can't be instantiated.
This is by itself not wrong, though it can be confusing to the new user.
I think it is like a trap, please see this inheritance.
class Labrador_Retriever : public Dog
{
public:
void eat() {}
};
if I just inherite from base class and I don't define it's object, that
means I can't find my error till the very time. I think that's amazing.
Hmmm, I think if you try to instantiate that class the compiler will tell
you that it can't because speak() isn't implemented. It will also point you
to the place that speak() was declared.
So far, I can't find anything wrong with this behaviour, but I agree that it
is something you have to get used to. In general, C++ is not 'nice'. If you
make a mistake, you will feel it, but that's the price you pay for the
power you wield with this language.
Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite
Sator Laser GmbH
Gesch??ftsf??hrer: Michael W??hrmann, Amtsgericht Hamburg HR B62 932