0

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

user7396627
  • 105
  • 2
  • 8
  • 1
    I understand that some people think that this is a poor question to ask. Could anyone tell me why that might be? – user7396627 Mar 29 '17 at 00:32
  • also, for those who are interested, this is related: http://stackoverflow.com/questions/5019046/reference-to-abstract-class – user7396627 Mar 29 '17 at 01:51
  • The reason why this is a bad question (or at least one of), is because this site is for asking and answering specific questions. When you know absolutely nothing about a language and haven't made much effort to learn, it's very hard to answer your questions specifically. What you need to do is pick up an introductory C++ book, and start reading it. And then ask about what you don't understand. That book will cover this exact topic and get you started on an understanding of it so you can ask a specific question that's possibly to answer here. – Nir Friedman Mar 29 '17 at 02:05
  • @NirFriedman Ah, ok, I see, I thought theory questions were answered here. Thanks for the clarification – user7396627 Mar 29 '17 at 05:16
  • 2
    Some people just don't like beginner questions. It's not a question of someone who "knows absolutely nothing about a language" because pointers on interface is in most beginner books qualified as an "advanced" subject. – ThePhi Nov 24 '20 at 08:31

5 Answers5

1

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.

John3136
  • 28,809
  • 4
  • 51
  • 69
1

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.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
0

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.

Jesse Hall
  • 6,441
  • 23
  • 29
0

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.

Daniel Kamil Kozar
  • 18,476
  • 5
  • 50
  • 64
0

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.

aschepler
  • 70,891
  • 9
  • 107
  • 161
  • "only or mostly" should perhaps say "at least one", though even that isn't strictly required. It's perfectly conceivable to have default implementations for all parts of an interface (though perhaps it is poor style to not be abstract regardless). – Kerrek SB Mar 29 '17 at 00:31