Re:Can you use a class in C?
by tragomaskhalos on Sunday August 13, 2023 @06:38AM
(#37108595)
Your allocation method - malloc + cast - may work for simple classes but overall it's a serious no-no; think you should either be using new/delete, or, if sticking with malloc /free, use placement new and an explicit destructor call respectively.
[ Parent
| on HN ]
The article discusses constructors and how to make them work under "Linking the C++ standard library".
I wonder if placement new would run into the same linker problem that the article mentions -- I'll have to try it at some point :)
And the stupidest thing, he uses this malloc in a .cc file, where it would no issue to just use new. Thats the point where I stopped reading. After
auto main() -> int {
...
}
I didn't supposed that anything really meaningful can come...