Data structures and Algorithms in C/C++11
github.com
github.com
class Tree {
public:
...
~Tree() { ...
...
If you have create a non-final class with a public non-virtual destructor, you are basically guaranteed that code using your library will have memory leaks.:(
class B : public A { ... } void foo() { A* someA = new B(...); ... delete someA; }
if A destructor is not virtual, then B's resources leak.
http://stackoverflow.com/questions/461203/when-to-use-virtua...
class Base {
public:
Base() {}
~Base() {}
};
class Derived : public Base {
private:
Foo* _foo;
public:
Derived() : _foo(new Foo) {}
~Derived() { delete _foo; }
};
int main() {
std::unique_ptr<Base>(new Derived);
}
The problem here is that if the destructor is not marked virtual, then when unique_ptr<Base> goes to invoke the destructor, it calls ~Base instead of ~Derived. That has no knowledge of Derived's member variables, so any memory that Derived allocated leaks. (Technically, it's undefined behavior and can do whatever it wants, but most implementations in practice will call ~Base.)It appears to be mixed. The files ending in .c contain C, the files ending in .cpp contain C++. See the Makefile, which will compile each with the relevant compiler.
Also, did you reply to the right parent?
Also, is it considered idiomatic to not leave any commentary breadcrumbs on what files and classes and structures are for? I get that lists.h probably contains lists, and I'm guessing linked lists from the code. The rest of it is guesswork.
Also, the two languages, while certainly not the same language, do have parts in common, and do interoperate decently well together.
As a consequence of using all the abstraction that C++ provides, one cannot simply dive right into C and expect the same level of productivity.
Absolutely true. What I meant by "interoperate" is that it's decently easy to call C functions from C++; the other way is also possibly, but you more or less have to pass C types around, which limits productivity somewhat.