Polymorphism
Polymorphism does make use of inheritance but it means so much more; the ramifications of polymorphism go much further then inheritance. Polymorphism allows for objects to have dynamic behavior based on subclasses.
To use polymorphism in C++ you need to use pointers or references and virtual methods. In C++ an object may have a static type and a dynamic type associated with it. The static type is the type associated at compile time; the dynamic type is associated at run time. For example: SomeClass *obj; This shows that obj is a pointer of type SomeClass. At runtime the obj may assigned to point to subclasses of SomeClass thus allowing for dynamic functionality. If a subclass's method is not declared virtual then the base class method will be called. Its important to make the destructor of a subclass virtual so that both the base class and subclass's destructor are called.
You dont want to declare methods as virtual needlessly. There is a cost associated with virtual methods; everytime a virtual method is called a look up table must be accessed to determine the objects functionality thus adding overhead.
Pascal Aschwanden
To use polymorphism in C++ you need to use pointers or references and virtual methods. In C++ an object may have a static type and a dynamic type associated with it. The static type is the type associated at compile time; the dynamic type is associated at run time. For example: SomeClass *obj; This shows that obj is a pointer of type SomeClass. At runtime the obj may assigned to point to subclasses of SomeClass thus allowing for dynamic functionality. If a subclass's method is not declared virtual then the base class method will be called. Its important to make the destructor of a subclass virtual so that both the base class and subclass's destructor are called.
You dont want to declare methods as virtual needlessly. There is a cost associated with virtual methods; everytime a virtual method is called a look up table must be accessed to determine the objects functionality thus adding overhead.
Pascal Aschwanden

0 Comments:
Post a Comment
<< Home