tags:

views:

138

answers:

6

What are the advantages of using C++ based Classes/Objects over using legacy C Structures for managing standard Data Structures like Linked List, Stack, Tree, Queue ..etc? Let me know if the question is not clear.

+4  A: 

For one thing, in C++ you can use templates, which allows you to create generic collections and use them in a type-safe manner. I.e. you can have a list of strings, another list of ints and a third of YourClass objects, all backed by a single list implementation, and the compiler guarantees that you can't put a string into a list of ints.

In C you could only achieve this via void* pointers, and type safety would not be guaranteed by the compiler.

Péter Török
Though not type-safe you can get all the goodness that C++ templates have to offer ( minus the hard stuff ) in C by using a pointer to void.
ardsrk
Performance would also suffer - consider that `std::deque<T>` can store its members mostly contiguous, but a `void*`-based solution would not.
MSalters
@ardsrk, sure you can implement similar stuff in C as well - usually with more effort, more code, and worse results. STL implementations have been tested and fine tuned for well over a decade now.
Péter Török
You could copy elements (eg. using `memcpy`) into contiguous buffer to achieve same results in C.
el.pescado
And yet more applications that require performance guarantees have been written in C than in C++. One example is Redis [ http://redis.io/ ]
ardsrk
@ardsrk, let's not start a language war :-)
Péter Török
@ardsrk: That's probably because a lot of existing code was written in C before C++ became [email protected]: You could, but it would be hideously slow and non-type-safe, as well as requiring extra memory for things like type size. Oh, and it would be a bunch of extra code to bug out, etc.
DeadMG
@ardsrk: you are assuming that the people who wrote those C applications actually had the choice. That high quality C++ compilers were available to them, that they had the C++ skills necessary, and so on. It's a ridiculous argument. If you can't say *why* C would be faster than C++, don't try to weasel your way out of it by saying "yeah but a lot of high perf apps have been written in C so it *must* be the fastest, right?" When confronted with factual arguments for why a template solution is faster than void*, you're going to need factual arguments if you're going to disagree.
jalf
A: 

One big advantage is that it provides you performance guarantees. Also, the code is easier to maintain as it is standardized and any new member joining the team will be able to understand the code quickly.

Naveen
A: 

Advantages of C++/OO data structures:

Natural grouping of data structure with methods that manipulate the structure.

Much more natural names for methods/functions dealing with the data-structure: e.g. push() and pop() rather than some artificial namespace-embedding function names.

Compiler-checked protection of data structures.

Inheritance - deal with any iterable sequence.

Douglas Leeder
A: 

the main advantage is that the methods (the code) are together with the data.

c style:

linkedlist_append(linkedlist* list, void* element)

now compare to:

class LinkedList
{
   LinkedList* Append(void* element);
}

or even

   LinkedList* Append(T* element);
Andrey
A: 

C++ data structures are provided by the STL (all the ones in your OP anyway). You can make yourself a std::map right away in any C++ compiler. In C, you'd have to write it yourself. In addition, the STL makes many guarantees like strong type safety, no memory leaks, and is often faster than home-rolled alternatives. In addition, in C++, you can write templated functions to take virtually any STL data structure and, say, iterate over it.

Basically, C++ will automate an incredibly large amount of work for you and guarantee that it works and works well. There's no advantage whatsoever to a C solution.

DeadMG
A: 

While you can add custom methods using just C, this is rather ugly and C++ provides a more elegant and natural way. In C++, you can use polymorphism and add a base class to your structures. In your example, all datastructures will probably support an add() and maybe a find() method. Therefore, you can write code which is independent of the underlying datastructure - you can change the implementation or even the used datastructure later, if desired. Additionally, e.g. a stack can be defined as a special list, therefore you can extend your list to be a stack (or queue) and thus you reduce redundancy in your code.

Also, C++ comes already with most useful datastructures in the standard library, which is well written, template based thus generic, and everywhere the same, thus portable.

zerm