Downcasting To Reach Members A superclass with many subclasses. But C++ does provide syntax for downcasting, suggesting that it is sometimes needed although it is potentially dangerous. Memory is never blank or empty - it always contains some random bit pattern - but the contents are unrelated to an Employee object and don't have any meaning when accessed in this way. Changing an object into a different one allows the compiler to "locate" data that is not there. The potential problem with downcasting is that it may turn an object into something it is not. However, being an Employee pointer, it also allows the compiler to access the Employee members, which were never created because the program instantiated a Person object! Attempting to use the phantom members will produce unpredictable and completely incorrect results. The variable cto is a pointer to an Employee, which allows the compiler to access the Person members, which is completely safe. The statement instantiates a Person (superclass) object but downcasts it to an Employee (subclass) object. Alternatively, downcasting can create phantom members.Notice that the Employee entry has a pointer to the Person entry. Symbol table entries for two classes, Person and Employee.Aside from loosing access to the Employee members, the upcast causes no harm. The Employee object is upcast to a Person, which makes the id and phone members unreachable. The statement instantiates an Employee object (the large rectangle) that has five member variables - three that belong to Person (the blue part) and two that belong to Employee (the green part).However, when the compiler looks up class Person, it only finds information about Person's member's but but cannot find information Employee or its members. So when the compiler looks up class Employee in its symbol table, it finds information about an Employee's two member variables, and, by following the inheritance link in the symbol table, it is also able to find information about Person and its three members. Recall that inheritance is a unidirectional relationship: a subclass "knows about" its superclass but a superclass "doesn't know about" its subclasses. downcasting: Why upcasting is safe but downcasting is not. Person* ceo = new Employee(.) Įmployee* cto = (Employee *)new Person(.) But this time, we explore the consequences of downcasting in greater detail. To do this, we return to the problem introduced in the previous section of locating member variables stored in objects. Let's first look at casting mechanics to help us understand why upcasting is safe while downcasting is not. There are two ways we can approach casting safety: mechanics and meaning. Knowing the consequences of casting will help us understand why one is safe and the other is not and why programs must sometimes perform a downcast. an upcast may make it impossible for a program to access subclass member variables, even though the object has them, without doing a downcast.a program can cause an upcast with an assignment, but upcasts typically result from a function call.downcasting, converting from a superclass to a subclass, requires an explicit cast operation.upcasting, converting a subclass to a superclass, can take place automatically, without an explicit casting operation.programs most often perform upcasts with pointers or references.casting is only possible between classes related by inheritance.So, let's review what we have learned about casting: Nevertheless, understanding the mechanics and consequences of casting will help us understand polymorphism and how to use it. With all the attention given to casting, it's easy to forget that our ultimate goal is understanding polymorphism. The compiler-created assignment operator function (a).Assignment (the behavior of object instantiated from classes is the same as objects created from structures).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |