Why doesn't polymorphism work without pointers/references?

前端 未结 6 1333
萌比男神i
萌比男神i 2020-11-22 03:09

I did find some questions already on SO with similar title- but when I read the answers they were focussing on different parts of the question which were really specific (e.

相关标签:
6条回答
  • 2020-11-22 03:45

    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.

    0 讨论(0)
  • 2020-11-22 03:48

    I found it really 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.

    0 讨论(0)
  • 2020-11-22 03:49

    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.

    0 讨论(0)
  • 2020-11-22 03:52

    "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.

    0 讨论(0)
  • 2020-11-22 03:53

    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.

    0 讨论(0)
  • 2020-11-22 04:03

    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.

    0 讨论(0)
提交回复
热议问题