Hello - I have a question about inheritance and polymorphism in C++. If one is making heavy use of inheritance and using a lot of pure virtual and plain virtual functions, are there any common practices for avoiding having the virtual functions staggered in confusing ways.

By that I mean having multiple classes in an inheritance chain and having overrides of virtual functions at different places along the chain.

Thanks,

Mike

Yes. Never override any implementation with a different implementation. Only override pure virtual functions. Use composition to parameterize your instances instead of using inheritance -- "never" use protected virtual functions, overriding them in subclasses, to parameterize a class's behavior. Instead, make a separate datatype that performs the method's computation and pass it in as a parameter.

You should never be making "heavy" use of inheritance. (But I'm not sure what your standards of "heavy" are.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.