Here's the problem: I need to implement a stack as an adapter to a List class. I don't know what that means though! I've researched on Google countless times and still don't understand what an adapter is. All the examples I've seen point to using inheritance and then using the inherited functions to perform actions in the adapter class, but I don't know how to do it myself. It just feels wrong that all I'm doing is taking the base class functions and using them for the adapter implementation without the adapter having any original implementation of its own. Can someone help? This is really frustrating me.

An adapter is really nothing more than a wrapper around another class that exposes a specialized interface. So at its simplest, a stack might be implemented like so:

template <typename T>
class Stack
{
public:
    bool empty() const { return _data.empty(); }

    void push(T const& x) { _data.push_back(x); }
    void pop() { _data.pop_back(); }

    T const& top() const { _data.back(); }
    T& top() { _data.back(); }
private:
    list<T> _data;
};

So, I understand your code. But, how would I implement the member functions?

By how, I mean do I create my own implementation or use the implementation provided in the adaptee class?

So, I understand your code. But, how would I implement the member functions?
By how, I mean do I create my own implementation or use the implementation provided in the adaptee class?

You say you understand my code, but your question suggests otherwise. Clearly the member functions are implemented by calling relevant functions from the adaptee. Usually this is just a simple call as in my code. Sometimes it's multiple calls, but you're not rewriting anything that the adaptee already does.

OK. So basically, as many places have said, an adapter class is similar to a "translation class", taking calls from one class and translating to another. When I implement my adapter functions, I use functions present in the adaptee class to do so. Does that sound right?

Sounds good to me.

Ohhhhhhhh...your code explained everything perfectly! I had to re-read it again; I'm not used to one-line code lol

By the way, thanks for the help! I really appreciate it :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.