Re: About pure virtual function
Lorry Astra wrote:
Here I'd like to quote a segment of code from Thinking In C++ (a book
writen by Bruce Eckel) to expain my question.
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". 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?
"Trap"? No. The life is simpler. The class 'Pet' is *abstract*.
You cannot instantiate it (create a stand-alone object of that
class). The class 'Dog' is also abstract (since it inherits two
pure virtual functions but only "de-purifies" one of them, so it
does *still* have one pure virtual function). Now, *unless* you
try to instantiate 'Dog', the compiler should not tell you anything
because it's not an error to have a pure virtual function in your
class. It's only illegal to instantiate it.
I think it is like a trap, please see this inheritance.
class Labrador_Retriever : public Dog
{
public:
void eat() {}
};
Huh?
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.
Till what very time? "Amazing"? Are you complaining because the
compiler does not explicitly tell you that you have abstract
classes in your program?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask