I need some samples of bad C++ code that will illustrate violation of good practices. I wanted to come up with my own examples, but I am having a hard time coming up with examples that are not contrived, and where a trap is not immediately obvious (it's harder than it seems).
Examples would be something like:
std::auto_ptr
members, and using std::auto_ptr
members with forward-declared classes.boost::shared_ptr
.size_t
and int
)....or any other evil thing you can think of.
I'd appreciate some pointers to existing resources, or a sample or two.
Code that are not exception safe can fail in ways that are not obvious to the readers of code:
// Order of invocation is undefined in this context according to the C++ standard.
// It's possible to leak a Foo or a Bar depending on the order of evaluation if one
// of the new statements throws an exception before their auto_ptrs can "own" it
accept_two_ptrs(std::auto_ptr<Foo>(new Foo), std::auto_ptr<Bar>(new Bar));
void MyClass::InvokeCallback(CallbackType cb)
{
Foo* resource = new Foo;
cb(resource); // If cb throws an exception, resource leaks
delete resource;
}
#include <iostream>
class Base
{
public:
virtual void foo() const { std::cout << "A's foo!" << std::endl; }
};
class Derived : public Base
{
public:
void foo() { std::cout << "B's foo!" << std::endl; }
};
int main()
{
Base* o1 = new Base();
Base* o2 = new Derived();
Derived* o3 = new Derived();
o1->foo();
o2->foo();
o3->foo();
}
And the output is:
A's foo!
A's foo!
B's foo!
Not sure if it has a name, but it sure is evil! :P
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With