user7396627
user7396627

Reputation: 105

Pointers to Interfaces c++

Disclaimer: I am a complete C++ beginner, and if there is a similar answer to this question, please direct me to it, as I may have missed it, not knowing much in the way of theory.

So I was looking through some c++ code, and I found something like this:

shared_ptr<anObjectsInterface>

Which got me wondering, how can there be a pointer to an interface? Surely a pointer points to an object in memory, or am I reading something wrong here.

Thank you

Upvotes: 0

Views: 7604

Answers (5)

Kerrek SB
Kerrek SB

Reputation: 477368

Don't forget that in the C++ object models, objects are composed of subobjects, which include base subobjects and member subobjects. An abstract class, or more generally an "interface" as you might call it, may not itself be a complete object, but it may well be the subobject of a "more complete" object. And pointers can point to arbitrary objects, including subobjects.

Schematically:

 +-- class Der --------------------------------------+
 | +---------+  +---------+ +----------+ +---------+ |
 | |*********|  |*********| |**********| |*********| |
 | +- int x -+  +- Base1 -+ +- bool b -+ +- Base2 -+ |
 +---------------------------------------------------+
 ^ ^            ^     
 | |            |
 | +-- &a.x     +-- static_cast<Base1*>(&x)
 &a;

Here we used:

struct Base1 { virtual void f() = 0; };
struct Base2 {};
struct Der : Base2, Base1 { int x; bool b; void f() override {} };

Der a;

The entire purpose of the virtual function dispatch mechanism is that you can call a (virtual) member function on an object that may not be a most-derived, but we will look up at runtime whether we are a subobject of a more-derived object, and we will use that object's class's implementation of the function.

Upvotes: 1

aschepler
aschepler

Reputation: 72431

In C++, "interface" is just a non-technical term for a class containing only or mostly pure virtual functions. An implementation of that interface will inherit the interface class, and the interface class has a "real" base class subobject within the complete object. The base class subobject has an address and occupies memory like any other object, so we can have a pointer to it.

Upvotes: 0

Daniel Kamil Kozar
Daniel Kamil Kozar

Reputation: 19306

anObjectsInterface is most possibly an abstract base class created exactly for a purpose like this : in order to pass an object around without having to specify which class actually implements the abstract class' methods.

A minimal example (using the naming you provided) would be something like this - you can find tons of these in any tutorial that mentions class hierarchies and/or virtual functions :

#include <memory>

struct anObjectsInterface {
  virtual int doStuff() const = 0;
};

struct anImplementation : public anObjectsInterface {
  int doStuff() const override {
    return 5;
  }
};

void doStuffWithObject(const std::shared_ptr<anObjectsInterface>& x) {
  x->doStuff();
}

int main() {
  auto object = std::make_shared<anImplementation>();
  doStuffWithObject(object);
}

You'll see that doStuffWithObject takes a std::shared_ptr<anObjectsInterface> and thus has no knowledge that this shared_ptr actually points to an instance of the anImplementation class.

Also, keep in mind that using shared_ptr in a scenario like this is needless : a unique_ptr would be much more fitting.

Upvotes: 0

Jesse Hall
Jesse Hall

Reputation: 6797

You're right that this shared_ptr will always either be empty or will point to a concrete object. But the specific type of that object is unknown to the shared_ptr; all it knows is that whatever object it points to implements (is derived directly or indirectly from) the anObjectsInterface class.

Upvotes: 0

John3136
John3136

Reputation: 29266

Obviously the pointer points to an object at a memory location. In this case it is supposed to point to some object that implements a particular interface.

Upvotes: 1

Related Questions