4 ms·
Sure it is possible. Just pass the constructor your type: my_type* t = New( MyType ) ; Where MyType passes information to New() of what to create. ... I don
by cdevfr 12y ago
Sure it is possible. Just pass the constructor your type:
my_type* t = New( MyType ) ;
Where MyType passes information to New() of what to create.
...
I don't know what I'm downvoted.
This clearly works. The naive solution without macros would have MyType be a simple integer and New() has a switch statement. But it is easy to avoid that.
- pmr_ 12y agoYou are being downvoted because the language comes with well-defined and specifically designed ways to customize allocation/deallocation. Your approach choses to ignore those facilities and even adds a runtime decision to code that absolutely has no need for it. You are reinventing the wheel and it is not even round.
- pjc50 12y agoBecause what you've written doesn't make much sense? You can't pass a type as a function parameter to a user defined function, and if you mean the builtin "new" (no capital) then that uses the normal allocator.
- cdevfr 12y agoIt is basic OOP in C. Look it up.
- fla 12y agoI think he meant that MyType is an enum and New is a user-defined function with a huge switch that does allocate (with placement new?) the correct type based on the value of MyType. Yea works, but introduces runtime overhead and is the gate to maintenance hell.