When an object A has a reference of an object B that was upcasted does that mean the object A has a reference of the B the upcasted object or does it have the original of class B before it was upcasted?
The class of the variable B dosen’t change. It remains the original class as set in A.
Upcasting will give you the object of the child class but you’ll have to set it in variable C of the correct class if you want to use it without casting again. You can often end up with multiple variables of different classes pointing to the same object to avoid casting…
…or use interfaces.
Thanks but i meant in the memory, when A have the reference of B ( B was upcasted )
Does A have only the reference of B or does it have the reference of B and the class that was upcasted to become B?
In memory your reference is only a memory address pointing to the start of your object. You (the programmer) says what type/class it is in order be able to use the data at the address. In reality you can use the object at that memory address as any class you like. Casting actually does exactly that - it returns the same memory address with different type associated with it. There are no changes done to the object itself when casting.
Then why do people say “avoid casting” if in order to use interfaces you need a reference ( even if it was upcasted like “try get pawn owner” in the animation ) ,
So it doesnt matter if you downcast it to the character , it will be loaded in memory anyways
Yep. Casting has no relation to memory. Your whole object will be loaded in memory regardless of what class you cast it to. I’ve never felt the need to downcast explicitly. Usually the downcast happens automatically when I save the object reference to a variable of the parent class. This operation is safe and invisible.
People saying “avoid casting” is kind of taken out of the context. (similar to “avoid using tick”) You should avoid excessive casting and there are good reasons for it:
- Cost
Casting has some inherit cost. If you find yourself casting to the same class often (like in tick or in a loop) you will save CPU time and some unnecessary logic if you do the cast up-front. I think casting is less expensive than normalizing a vector though and I’ve seen those done everywhere. - Coupling
Once you cast to a specific class in another class, you tie the classes together. Now the latter class can’t be used independently - it always needs to know all about the class it is casting to. This problem is alleviated by interfaces. With interfaces, both classes can depend on a lightweight interface that has no data and no implementation. (hence it’s much easier to extend and modify) - Bad Design
The general consensus is that casting is a mark of bad software design. The argument goes something like this: “You change the type by casting. Why? Why is your code not written so the correct type is used from the start?” I’m not sure how valid is this argument in the case when most of the code and classes are provided by the engine itself but I get the sentiment.
For a discussion on the topic done by programmers (not weekend coders like me)
Ohh alr thanks alot
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.