91 points · by vickychijwani · 12y ago · 46 comments
#6 sometimes you actually will want an A(A&) constructor... for example, if you have constructor overloaded with a greedy template argument.
#8 objects returned by value will also bind to an rvalue reference
#9 Remember in C++11 there's also new T{};
#10 isn't absolutely correct either, dynamic_cast can actually be used in some circumstances where static_cast or implicit casting could have been used.
struct A {
};
struct B: A {
};
int main() {
auto b = new B();
auto a = dynamic_cast<A*>(b); // fine
}There are reasons why you might want to use public inheritance that have nothing to do with run-time polymorphism.
It seems like, if you're not providing a virtual destructor, you force users into delegation rather than inheritance, and that will catch some % of developers who aren't on the ball. They'll have leaks...
As far as i know (old info) there's no such thing as a "final" class, so anybody can inherit even if you don't want them to.
You have the `final` specifier since C++11 to state a class cannot be inherited from, but this notion of "You really shouldn't inherit from this class." has been around since classical object orientation has, C++ just couldn't express it as a language.
So if your class currently isn't being inherited from, make your public dtor nonvirtual. If and when it needs to be a base class, then the author can modify your class to have a virtual dtor, or use composition (which is often the superior alternative). If source code is not available, then composition it is. I would advise against making it virtual "just in case", that's premature pessimisation.
Anyone can technically publicly inherit from a value class, but if they do it's absolutely their fault :)
Slicing isn't even necessarily harmful either... but I'm not sure what you had in mind.
struct A {
A (A const&); // copy constructor;
A (A&&); // move constructor
template <typename T> A (T&&); // template constructor
};
int main() {
A a1;
A a2(a1); // calls the template constructor because a1 is an lvalue
}You're exactly right. But there are multiple complications:
- unique_ptr and shared_ptr are only available in C++11 onwards, we're still to migrate to a new compiler
- auto_ptr is a joke [1]
- I've got to live with `delete` in our gargantuan codebase at work
- I could (and should) use boost's smart pointers, and indeed I do when possible. But I admit I sometimes even forget to use those and end up with naked pointers instead.
And if you really can't, and you need a container that automatically frees its contents when destroyed, just stick it in a STL vector.
std::unique_ptr (or its predecessor, boost:scoped_ptr if you don't have access to C++11) should be used all over the place. There are many reasons why you may not want by-value containment of objects, and std::unique_ptr gives you object-lifetime ownership of pointers.
You don't always have control over the code you are writing, an example that comes to my mind quickly is ffmpeg.
Smart pointers are not a panacea.
http://clhs.lisp.se/Issues/iss152_w.htm
"The ambiguity at issue arises for the case where there are transfers of control from the cleanup clauses of an UNWIND-PROTECT. ..."
It is not clear what exit points are visible when the cleanup-forms (morally similar to C++ destructor calls) are invoked. Are the exit points that are skipped torn down all the way to the control transfer's target, or is the tear-down interleaved with the unwinding.
You might need to do this if you're implementing your own smart handle or writing a factory function for a class with a private constructor (make_unique will not work).
Off the top of my head, I can't think of any good reasons to use delete.
> If you'll tolerate my hypocrisy for a moment, here's my suggestion: try to avoid putting the const at the beginning like that
Or you could not do that, and put const in the least ambiguous place, and since you rarely have a 'type* const ' it works very well just using:
const type*
for pointers to const types, and type* const
for const pointers to types, and entirely unambiguous.Shouldn't rvalue move semantics kick in and prevent the copy?
Remember this rule and the throws behavior makes perfect sense, as do a lot of other C++ gotchas.