88

I did find some questions already on StackOverflow with similar title, but when I read the answers, they were focusing on different parts of the question, which were really specific (e.g. STL/containers).

Could someone please show me, why you must use pointers/references for implementing polymorphism? I can understand pointers may help, but surely references only differentiate between pass-by-value and pass-by-reference?

Surely so long as you allocate memory on the heap, so that you can have dynamic binding, then this would have been enough. Obviously not.

2

6 Answers 6

80

"Surely so long as you allocate memory on the heap" - where the memory is allocated has nothing to do with it. It's all about the semantics. Take, for instance:

Derived d;
Base* b = &d;

d is on the stack (automatic memory), but polymorphism will still work on b.

If you don't have a base class pointer or reference to a derived class, polymorphism doesn't work because you no longer have a derived class. Take

Base c = Derived();

The c object isn't a Derived, but a Base, because of slicing. So, technically, polymorphism still works, it's just that you no longer have a Derived object to talk about.

Now take

Base* c = new Derived();

c just points to some place in memory, and you don't really care whether that's actually a Base or a Derived, but the call to a virtual method will be resolved dynamically.

11
  • I thought slicing was to do with liskovs substitution principle, not pointers? So Derived d = new Base() would cause slicing because a Base is not a Derived.... but a Derived is a Base. So what is it which the pointer does which makes it a derived class (you said "polymorphism doesnt work because you no longer have a derived class"- why?)
    – user997112
    Mar 3, 2013 at 18:21
  • @user997112 when you construct Base c = Derived() (which may or may not work, depending on what functions you have implemented), c is still a Base and has only the memory layout and instance variables of a Base object. (For one thing, Derived might be larger than Base.) Every variable in C++ has a specific amount of storage allocated to it at compile-time based on its type (queryable using the sizeof operator) and cannot simply change to another type with another size. Mar 3, 2013 at 18:29
  • 3
    @user997112: But a pointer can change its value so that it holds the address of a different object, possibly of a different class, with a different size. An object however, cannot change to be a different object. It cannot change its own address. Mar 3, 2013 at 18:49
  • 2
    @user997112 when you call a virtual function on any object (directly or through a reference or pointer), it's always based on the runtime (dynamic) type of that object; however, C++ semantics guarantees that every object variable (not a pointer or reference) in a well-formed program always has the type it was declared as at compile time (this is a design decision). However, a pointer or reference to a type is allowed to point to any object of the declared type or any subtype (this is also a design decision, it's possible they could have made pointers and/or references non-polymorphic)... Mar 3, 2013 at 19:24
  • 2
    ...so when you call a function through a pointer or reference, the compiler inserts code that checks the actual runtime type (more or less) and executes the correct function. This never happens with an object variable because an object variable is never supposed to change its type. This is just how the object model works and it's a very effective one. Mar 3, 2013 at 19:25
62

In C++, an object always has a fixed type and size known at compile-time and (if it can and does have its address taken) always exists at a fixed address for the duration of its lifetime. These are features inherited from C which help make both languages suitable for low-level systems programming. (All of this is subject to the as-if, rule, though: a conforming compiler is free to do whatever it pleases with code as long as it can be proven to have no detectable effect on any behavior of a conforming program that is guaranteed by the standard.)

A virtual function in C++ is defined (more or less, no need for extreme language lawyering) as executing based on the run-time type of an object; when called directly on an object this will always be the compile-time type of the object, so there is no polymorphism when a virtual function is called this way.

Note that this didn't necessarily have to be the case: object types with virtual functions are usually implemented in C++ with a per-object pointer to a table of virtual functions which is unique to each type. If so inclined, a compiler for some hypothetical variant of C++ could implement assignment on objects (such as Base b; b = Derived()) as copying both the contents of the object and the virtual table pointer along with it, which would easily work if both Base and Derived were the same size. In the case that the two were not the same size, the compiler could even insert code that pauses the program for an arbitrary amount of time in order to rearrange memory in the program and update all possible references to that memory in a way that could be proven to have no detectable effect on the semantics of the program, terminating the program if no such rearrangement could be found: this would be very inefficient, though, and could not be guaranteed to ever halt, obviously not desirable features for an assignment operator to have.

So in lieu of the above, polymorphism in C++ is accomplished by allowing references and pointers to objects to reference and point to objects of their declared compile-time types and any subtypes thereof. When a virtual function is called through a reference or pointer, and the compiler cannot prove that the object referenced or pointed to is of a run-time type with a specific known implementation of that virtual function, the compiler inserts code which looks up the correct virtual function to call a run-time. It did not have to be this way, either: references and pointers could have been defined as being non-polymorphic (disallowing them to reference or point to subtypes of their declared types) and forcing the programmer to come up with alternative ways of implementing polymorphism. The latter is clearly possible since it's done all the time in C, but at that point there's not much reason to have a new language at all.

In sum, the semantics of C++ are designed in such a way to allow the high-level abstraction and encapsulation of object-oriented polymorphism while still retaining features (like low-level access and explicit management of memory) which allow it to be suitable for low-level development. You could easily design a language that had some other semantics, but it would not be C++ and would have different benefits and drawbacks.

9
  • So are we saying that the run-time system will only make vtable lookups if it sees the object was declared with a pointer, and that is just how the C++ design was? Hence polymorphism only works with a pointer (or reference) because thats the only way the run-time will make a vtable lookup and hence perform polymorphism?
    – user997112
    Mar 3, 2013 at 21:03
  • 1
    It's not that the object is declared with a pointer, it's that you're calling a virtual function through a pointer or reference. An object is always instantiated as a complete type known at compile-time, whether it gets referenced by a pointer or reference later doesn't affect the object itself. (new returns a pointer to an object on the heap, but conceptually the object is still there with its compile-time type until it's destroyed) Mar 3, 2013 at 21:09
  • 1
    You can have object variables on the heap (if they're instance variables of a heap-allocated object, for instance) or pointers to objects allocated on the stack, the semantics are the same regardless. Mar 3, 2013 at 21:13
  • 1
    Just take a look at the first block of code in LuchianGrigore's answer, Base * b is a polymorphic pointer and happens to be pointing to an object of Derived which is on the stack. Mar 3, 2013 at 21:36
  • Stephen, do you know of any books that go into this sort of thing especially well?
    – Elliot
    Jun 20, 2015 at 14:40
17

I found it helpful to understand that a copy constructor is invoked when assigning like this:

class Base { };    
class Derived : public Base { };

Derived x; /* Derived type object created */ 
Base y = x; /* Copy is made (using Base's copy constructor), so y really is of type Base. Copy can cause "slicing" btw. */ 

Since y is an actual object of class Base, rather than the original one, functions called on this are Base's functions.

4

Consider little endian architectures: values are stored low-order-bytes first. So, for any given unsigned integer, the values 0-255 are stored in the first byte of the value. Accessing the low 8-bits of any value simply requires a pointer to it's address.

So we could implement uint8 as a class. We know that an instance of uint8 is ... one byte. If we derive from it and produce uint16, uint32, etc, the interface remains the same for purposes of abstraction, but the one most important change is size of the concrete instances of the object.

Of course, if we implemented uint8 and char, the sizes may be the same, likewise sint8.

However, operator= of uint8 and uint16 are going to move different quantities of data.

In order to create a Polymorphic function we must either be able to:

a/ receive the argument by value by copying the data into a new location of the correct size and layout, b/ take a pointer to the object's location, c/ take a reference to the object instance,

We can use templates to achieve a, so polymorphism can work without pointers and references, but if we are not counting templates, then lets consider what happens if we implement uint128 and pass it to a function expecting uint8? Answer: 8 bits get copied instead of 128.

So what if we made our polymorphic function accept uint128 and we passed it a uint8. If our uint8 we were copying was unfortunately located, our function would attempt to copy 128 bytes of which 127 were outside of our accessible memory -> crash.

Consider the following:

class A { int x; };
A fn(A a)
{
    return a;
}

class B : public A {
    uint64_t a, b, c;
    B(int x_, uint64_t a_, uint64_t b_, uint64_t c_)
    : A(x_), a(a_), b(b_), c(c_) {}
};

B b1 { 10, 1, 2, 3 };
B b2 = fn(b1);
// b2.x == 10, but a, b and c?

At the time fn was compiled, there was no knowledge of B. However, B is derived from A so polymorphism should allow that we can call fn with a B. However, the object it returns should be an A comprising a single int.

If we pass an instance of B to this function, what we get back should be just a { int x; } with no a, b, c.

This is "slicing".

Even with pointers and references we don't avoid this for free. Consider:

std::vector<A*> vec;

Elements of this vector could be pointers to A or something derived from A. The language generally solves this through the use of the "vtable", a small addition to the object's instance which identifies the type and provides function pointers for virtual functions. You can think of it as something like:

template<class T>
struct PolymorphicObject {
    T::vtable* __vtptr;
    T __instance;
};

Rather than every object having its own distinct vtable, classes have them, and object instances merely point to the relevant vtable.

The problem now is not slicing but type correctness:

struct A { virtual const char* fn() { return "A"; } };
struct B : public A { virtual const char* fn() { return "B"; } };

#include <iostream>
#include <cstring>

int main()
{
    A* a = new A();
    B* b = new B();
    memcpy(a, b, sizeof(A));
    std::cout << "sizeof A = " << sizeof(A)
        << " a->fn(): " << a->fn() << '\n';
}          

http://ideone.com/G62Cn0

sizeof A = 4 a->fn(): B

What we should have done is use a->operator=(b)

http://ideone.com/Vym3Lp

but again, this is copying an A to an A and so slicing would occur:

struct A { int i; A(int i_) : i(i_) {} virtual const char* fn() { return "A"; } };
struct B : public A {
    int j;
    B(int i_) : A(i_), j(i_ + 10) {}
    virtual const char* fn() { return "B"; }
};

#include <iostream>
#include <cstring>

int main()
{
    A* a = new A(1);
    B* b = new B(2);
    *a = *b; // aka a->operator=(static_cast<A*>(*b));
    std::cout << "sizeof A = " << sizeof(A)
        << ", a->i = " << a->i << ", a->fn(): " << a->fn() << '\n';
}       

http://ideone.com/DHGwun

(i is copied, but B's j is lost)

The conclusion here is that pointers/references are required because the original instance carries membership information with it that copying may interact with.

But also, that polymorphism is not perfectly solved within C++ and one must be cognizant of their obligation to provide/block actions which could produce slicing.

2

You need pointers or reference because for the kind of polymorphism you are interested in (*), you need that the dynamic type could be different from the static type, in other words that the true type of the object is different than the declared type. In C++ that happens only with pointers or references.


(*) Genericity, the type of polymorphism provided by templates, doesn't need pointers nor references.

3
  • I don't mean to split straws- but I am trying to understand (at a low level I guess) why this part of your message is so: "In C++ that happens on with pointers or references". Why is this the case?
    – user997112
    Mar 3, 2013 at 18:45
  • 1
    @user997112 because the point of having object-orientation is to have some static guarantees about object lifetimes and identities. if objects could arbitrarily change runtime types after construction, it would be much harder to maintain invariants about program state. Mar 3, 2013 at 18:56
  • @user997112, the object model of C++ doesn't lean that way. Mar 3, 2013 at 19:04
0

When an object is passed by value, it's typically put on the stack. Putting something on the stack requires knowledge of just how big it is. When using polymorphism, you know that the incoming object implements a particular set of features, but you usually have no idea the size of the object (nor should you, necessarily, that's part of the benefit). Thus, you can't put it on the stack. You do, however, always know the size of a pointer.

Now, not everything goes on the stack, and there are other extenuating circumstances. In the case of virtual methods, the pointer to the object is also a pointer to the object's vtable(s), which indicate where the methods are. This allows the compiler to find and call the functions, regardless of what object it's working with.

Another cause is that very often the object is implemented outside of the calling library, and allocated with a completely different (and possibly incompatible) memory manager. It could also have members that can't be copied, or would cause problems if they were copied with a different manager. There could be side-effects to copying and all sorts of other complications.

The result is that the pointer is the only bit of information on the object that you really properly understand, and provides enough information to figure out where the other bits you need are.

1
  • 9
    I won't -1 you but stack vs heap has no relevance here, it just adds to confusion to mention that. Mar 3, 2013 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.