954,157 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ Inheritance and Polymorphism

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

Dinomike1000
Newbie Poster
16 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

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.)

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 176
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You