If a struct is defined in a class, then if assigned a different value in the class method, the value retains
struct allocated on the Stack
class allocated on the Heap
struct is just a block of memory.
class is a struct, except it is allocated on the heap, needs a constructor, and needs to be deallocated.
Why doesn't struct have a constructor? Does it?
Where did the concept of the constructor come from?
Where did the concept of the heap come from?
The concept of the heap probably came from OOP. OOP came from getting rid of if else statements. Can you work up the example?
How does executing one method based on type alleviate if-else statements and maintenance hell?
Maintenance hell is caused by trickling code changes. One small change demands many other places in the code to be changed too. What is an example of that?
Think macros, it's one layer of indirection. OOP is that too. This is in that Objective-C book.
The indirection happens at the method black box. The method remains intact, with exact typed inputs and outputs, the object type can be changed at runtime with polymorphism. So methods help, but how does objects factor into alleviating maintenance hell? Objects are sets of methods and properties, you can keep the original implementation plus add new ones (inheritance). With the old way you can't. You would have to rename the function where new functionality is required, which is a trickling change. So objects allow us to easily keep old implementation at the same time add new ones, using inheritance.
I'm sure in the beginning language designers just added methods and properties to structs, that essentially creates encapsulation, however that wasn't enough. What does encapsulation offer by itself without inheritance and polymorphism? Modularization. It clearly separates one module from another, but if you had similar structs, you wouldn't be able to reuse the similar parts, you would have to copy and paste them.
Inheritance and encapsulation both indicate language constructs, what you're doing when you are writing code. Polymorphism indicates behavior, runtime behavior.
So questions remain:
- Where exactly is the indirection?
- Why is constructor needed?
- How does heap help realize polymorphism?
Answers:
- The indirection happens at runtime. The same code gets redirected to the proper class based on type,determined at runtime, using vtables. Since the same code doesn't change, we've achieved indirection.
- Construction is needed because of encapsulation. It's the only chance for assigning external values to private properties. Encapsulation is needed because of modularization. Modularization is needed because of code reuse. Code reuse is achieved by polymorphism.
-
Heap and polymorphism is unrelated. Heap = dynamic memory allocation, C has that (malloc).
- Polymorphism is realized with vtables in C++.
- A stack allocated object can't be persisted longer than the scope of the function.
- A heap object can be used by many different methods.
- In C++ structs can be inherited.
Conclusion:
Since polymorphism is about propagating code change without changing code, having heap objects polymorph rather than stack objects makes sense because they last longer throughout the code. Plus, syntactically there is no "newing" a stack object, so there is no place to declare the morphed (child) type. In a function, variables initiations are all localized in that scope, there is no chance to for the variable to morph unpredictably.