Why would that make destructors more dangerous? It seems to work okay in e.g. Rust.
-
-
Vastauksena käyttäjälle @ssylvan
Suppose I have a class that contains a handle to a ref-counted object. When the class gets destructed I want to call releaseRef(handle). Without a copy or move ctor, I could easily call releaseRef() more times than addRef().
1 vastaus 0 uudelleentwiittausta 0 tykkäystä -
Vastauksena käyttäjälle @tloch14
I don't think there's a practical difference. You can implement a method that gives you a handle (and increments the ref count for you), and implement an explicit copy method that correctly copies it (with addref).
1 vastaus 0 uudelleentwiittausta 0 tykkäystä -
So how are you going to call releaseRef more than addRef? By memcpying it to another instance maybe, but you can do that now too so it's not really a new issue.
1 vastaus 0 uudelleentwiittausta 0 tykkäystä -
Think about how this stuff works in C (except with explicit destructor calls). It's easy to hide implementation so that the only way to create or copy something is through a blessed (explicit) method, when that kind of restriction is warranted.
2 vastausta 0 uudelleentwiittausta 0 tykkäystä -
Vastauksena käyttäjälle @ssylvan
Right, but that puts the owness on the user to know he has to make this copy() call. When writing a generic array class, you're likely going to memcpy. Or you'll provide a copy function to the array class. Or have an overloaded copy() function all classes must do.
1 vastaus 0 uudelleentwiittausta 0 tykkäystä -
At what point are you asking just a language-supported copy operator? (thought I guess copy assignments still exist, it's mostly a matter of whether the destination has valid initial values)
1 vastaus 0 uudelleentwiittausta 0 tykkäystä -
Vastauksena käyttäjälle @tloch14
Rust has a Copy trait that you can opt into. That seems fine and could be the type of things you'd add as a consequence after you've removed the constructors. But if not.. eh, no big deal. Just write C like code. Can't copy if you only have a Foo* and don't know the size!
1 vastaus 0 uudelleentwiittausta 0 tykkäystä -
Vastauksena käyttäjälle @ssylvan
sizeof(Foo)? I suppose with a copy operator opt-in and default initialization, I can see constructors going the way of the dodo.
1 vastaus 0 uudelleentwiittausta 0 tykkäystä -
Vastauksena käyttäjälle @tloch14
How are you going to do sizeof(Foo) when Foo is defined in foo.cpp and you can't see it? That's how most C apis work. You get a forward declared Foo in the header and all functions take Foo*. Caller can't do anything with Foo that isn't exposed through API.
2 vastausta 0 uudelleentwiittausta 0 tykkäystä
That's how C apis work, but given the C++ language would still have methods, polymorphism, templates, etc, you couldn't rely on this being the case.
Lataaminen näyttää kestävän hetken.
Twitter saattaa olla ruuhkautunut tai ongelma on muuten hetkellinen. Yritä uudelleen tai käy Twitterin tilasivulla saadaksesi lisätietoja.