|
-
April 16th, 1999, 10:05 PM
#1
virtual base classes
In Marshals cline and Gregs Lomow's book C++ FAQ second edition FAQ21.11 says:
Can a pure virtual function be defined in the same class that declares it?
A: Yes, but new C++ programmers dont usually understand what it means, so this practice should be avoided... etc.
Can somebody give me an example of how one would define a pure virtual function?
thanks
-
April 16th, 1999, 11:45 PM
#2
Re: virtual base classes
In your base class:
virtual void MyPureVirtualFunction(void) = 0;
The voids can be any data type.
The use of a pure virtual function in the base class ensures that the complier will check for an override of that function in all the derived classes. If you forget to override it, it gives you a compile time error.
-
April 17th, 1999, 01:58 AM
#3
Re: virtual base classes
thanks for your post, but Im not looking for how to _declare_ a pure virtual function, im trying to understand what the C++ FAQs call _defining_ a virtual function in the same class that declares it. As in, you declare a pure virtual function like so:
virtual void Draw() =0;
can you define it? apparently so according to the book, how do you define it and why would you?
thanks
-
April 17th, 1999, 03:01 AM
#4
Re: virtual base classes
Usually, if you declare pure virtual functions in a class, that class will be an abstract class and you can't directly declare an instance of an abstract base class. However, you can implement these pure virtual functions in the class, but it's not necessary because the inherited classes have the responsibility to implement them if they want to use these virtual functions.
If you do implement these pure virtual functions, you still can't declare an instance of this abstract class. But, the inherited class can call the functions.
For example:
class A{
public:
A(){}
~A(){}
virtual void foo1()=0; // pure virtual function
virtual void foo2()=0; // pure virtual function
virtual void foo3()=0; // pure virtual function
};
// class A only implement foo1()
void A::foo1(){printf("class A: foo1");}
class B : public A
{
public:
B(){}
~B(){}
virtual void foo1(){A::foo1();}
virtual void foo2(){printf("class B: foo2");}
virtual void foo3(){A::foo3();}
};
// class B only implement foo2
// if you ..
A objA; // compile error: class A is an abstract class
// if you ..
B objB; // link error: foo3() not found in A
// if foo3 isn't declared in class B
B objB; // OK
objB.foo1(); // output --> class A: foo1
objB.foo2(); // output --> class B: foo2
// end
Therefore, the implementation for foo1 in class A is not necessary; otherwise, foo1() doesn't need to be declared as a pure virtual function.
That's my opion. Good luck.
Lynx
-
April 17th, 1999, 08:41 PM
#5
Re: virtual base classes
Thanks that cleared things up.
I can see why they recomend not to do it though, cant think of a single usefull use of this, and theres many ways to make an error. Why not just dissalow defining pure virtual functions then? hmm
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|