27-Sep-2014 12:11, Foo пишет:
Consider:

struct MyRefCounted
    void opInc();
    void opDec();
    int x;
}

MyRefCounted a;
a.x = 42;
MyRefCounted b = a;
b.x = 43;

What is a.x after this?


Andrei

a.x == 42
a.ref_count == 1 (1 for init, +1 for copy, -1 for destruction)
b.x == 43
b.ref_count == 1 (only init)

There is no implicit ref-count. opInc may just as well create a file on harddrive and count refs there. Guaranteed it would be idiotic idea, but the mechanism itself opens door to some cool alternatives like:

- separate tables for ref-counts (many gamedevs seem to favor this, also see Objective-C)
- use padding of some stuff for ref-count
- may go on and use e.g. 1 byte for ref-count on their own risk, or even a couple of bits here and there

I may go on, and on. But also consider:

GObject of GLib (GNOME libraries)
XPCOM (something I think Mozila did as sort-of COM)
MS COM
etc.

Refcounting is process of add(x), and sub(x), and calling destructor should the subtract call report zero. Everything else is in the hands of the creator.

--
Dmitry Olshansky

Reply via email to