betterC: new operator
Basile B.
b2.temp at gmx.com
Sun Nov 22 17:16:46 UTC 2020
On Sunday, 22 November 2020 at 16:58:03 UTC, Basile B. wrote:
> On Sunday, 22 November 2020 at 11:54:01 UTC, Dibyendu Majumdar
> wrote:
>> I had assumed that new operator is not available in betterC.
>>
>> But in following code, it seems the only way to initialize the
>> object correctly ... is there another way I am missing? How
>> should one cleanup objects?
> however note
> 1. not well tested (e.g overloads)
> 2. static init of fields is not done because that really
> rquires TypeInfo, so a ctor has to be used instead.
>
> But that's a good start to get things done more properly
And the leaks...
More simple solution adapted from
http://dpldocs.info/this-week-in-d/Blog.Posted_2020_07_27.html#zero-runtime-classes
---
#!dmd -betterC
extern(C) int printf(const char*, ...);
extern(C++) class A {
int omg() { return 12; }
}
extern(C++) class B : A {
override int omg() { return 34; }
}
template New(T) {
pragma(mangle, "_D" ~ T.mangleof[1..$] ~ "6__initZ")
__gshared extern immutable
ubyte[__traits(classInstanceSize, T)] initializer;
T New(ref ubyte[__traits(classInstanceSize, T)] memory) {
foreach(idx, ref b; memory) {
b = initializer.ptr[idx];
}
return cast(T) memory.ptr;
}
}
extern(C) int main() {
ubyte[__traits(classInstanceSize, A)] buffer;
A a = New!B(buffer);
printf("hi %d\n", a.omg());
return 0;
}
---
More information about the Digitalmars-d
mailing list