Home > database >  Are there analogues to Java's Collection interfaces in C ?
Are there analogues to Java's Collection interfaces in C ?

Time:09-18

I'm trying to make a doubly linked list in C , and I'm trying to do it right. Therefore, I thought I should adhere to any interface structure recommended by any "official" C guideline, such as an interface. In Java, there are such interfaces which one is free to use and which sort of standardise abstract data types (such as https://docs.oracle.com/javase/8/docs/api/java/util/List.html).

My question: Is there anything like this in C ? Startpageing found me this link:

https://en.cppreference.com/w/cpp/container/list

But it doesn't seem to use any inheritance.

CodePudding user response:

The standard library is designed in a way to avoid needing such inheritance in the first place.

More often than not, you can get essentially the same behavior as interfaces, without any of the runtime overhead of indirect calls, by duck-typing on the container type:

#include <vector>
#include <list>
#include <iostream>

template<typename Collection>
void do_something(const Collection& data) {
  for(const auto& v : data) {
     std::cout << v << "\n";
  }
}

int main() {
  std::vector<int> a;
  std::list<int> b;

  do_something(a);
  do_something(b);
}

If you are approaching this from a Java perspective, you can think of it as being "generics on steroids", where a function can generically accept anything that looks like a collection.

There are a few other approaches possible involving iterators and ranges, but the principles remain the same.

CodePudding user response:

Is there anything like this in C ?

Depends on what level of similarity you're looking for and where you're looking for it.

The Java collection interfaces are based on runtime polymorphism. In C terms, such API is based on inheritance and virtual member functions.

The containers provided by the standard library serve a similar function as the collections in Java. However, they do not provide an API based on inheritance and virtual member functions. Instead, their API is based on compile time polymorphism i.e. templates which are somewhat similar to generics in Java. In the analogy of Java collections and C standard containers, the concepts aka named requirements specified in the standard are analogous to the interfaces in Java.

It is possible to implement interfaces - such as they are in Java - in C . The C equivalents are simply abstract classes with no non-pure member functions. You must use virtual inheritance in order to share bases in the way that you can share interfaces in Java. It is also possible to implement a container API based on runtime polymorphism. But such container API is not provided by the standard library.


I'm trying to make a doubly linked list in C

Other than for exercise purposes you rarely need to, because the standard library already comes with an implementation of double linked list. It's the std::list whose reference page you've linked in the question.

and I'm trying to do it right.

"Right" is relative, but you'll likely do fine by copying what the standard library does.

If you do wish to follow the example shown by the standard library, then you should provide an API based on concepts (aka named requirements) and templates. Your class (template) should conform to the Container concept, and more specifically the SequenceContainer concept.

CodePudding user response:

Certainly, what type of language would not implement collections? However writing your own collection class is laborious.

You can #include<vector.h> and then use (for an example):

vector <int> c { 1, 2, 3 }; 
c.push_back(4); 
cout << c[2];

CodePudding user response:

In C , you can implement it as your own interface (keep in mind to add public before the actual implementation).

To make a great job, you can define your own abstract methods in you own abstract class, like:

class ExampleAbstractClass {
   public:
      virtual string myFunction() = "To be implemented";
   
      void notAbstractMethod(int aNumber) {
         aNumber = h;
      }
   
   protected:
      int aNumber;
};

class ExapleClass: public ExampleAbstractClass{
   // abstract implementation
   public:
      string myFunction() { 
         return "That's now customly implemented!"; 
      }
}
  • Related