Actually, old school objects are closer to how stack oriented C++ classes work.
Also note that C++ classes can be just as heap oriented as Object Pascal classes and in some serious application in libraries it is even preferred. (If you see a lot of
-> it is likely heap
)
An example of this is how C++Builder's non-standard libraries work (not only the delphi bindings). Another one is COM/XCOM (in C++ too:heap)
So really it is all a bit of confusion:
- C++ Classes are not necessary stack oriented, it depends on how they are written.
- Anything that has virtual methods or reference counted types (or invokes structured exception handling) or requires multi-threading application uses some heap anyway. C++ or Object Pascal.
- Stack oriented classes from C++ resemble old school Object Pascal Objects, which also favor stack over heap, depending on use, but see the above.
- If the class's constructor is invoked through new, assume it is heap, unless you know
new is overridden to use stackalloc. On old school objects: when created with new and dispose, it is likely heap.
Side note: there exists some Object Pascal code that has stack based memory management for
dynamically created old school objects too. an example can be found in the
implementation section of Delphi 3/4/5/6/7+ grids.pas unit for cell objects management. It is well hidden there, for obvious reasons...
It was factored out into a mm and published in UNDU.
Ref:
http://rmarsh.com/2005/08/28/objects-on-the-stack-a-delphi-relic/ That article is old, but very enlightening and a must read.
Also note using stack has some advantages for smaller classes (e.g. speed through automatic destruction at end of scope) it has also many disadvantages: e.g. stack size management, debugging, exception handling and thread-safety.
To summarize:
- C++ classes are
NOT necessarily stack oriented. But you have the
option to write them that way but that also limits their application.
- In object Pascal we have the
option of using old school objects in a stack oriented way, not only records. Same limitation.
- In both languages choosing the stack requires a lot of thought. Mixing stack and heap is actually not for beginners in both languages.
- The statement:
C++ classes are stack oriented as a generalization is
false.
note: the code from the link is not FreePascal ready and requires a small rewrite, because of differing memory manager structures in Delphi vs Freepascal.
(For user 440x: if you indeed like to factor out any reference counted types and use shortstrings, this may be a little toy for you
)