1

late-binding or runtime-polymorphism needs 2 things: a base pointer and a virtual method.

class FrameWorkClassBase {
  public:
    virtual void method() = 0;
};

class FrameWorkClassDerived: public FrameWorkClassBase {
public:
    virtual void method() override { ... }
}

class ProductionConsumerClass { // uses composition
  public:
    void method() {
        this->m_sptr->method(); // dynamic dispatch
    }
  
  private:
    // ptr to base class, composition at work
    std::shared_ptr<FrameWorkClassBase> m_sptr =
                              std::make_shared<FrameWorkClassDerived>();
}

there seems to be a bias towards using heap memory for polymorphic object creation rather than stack allocated objects, especially when it comes to using "polymorphic object" as component objects. Is this a requirement: is it the nature of the C++ language as a whole that forces the use of heap memory for "polymorphic component objects" ? Are there any design patterns that overcome the need for using heap memory for "polymorphic component objects" ?

Note: I do not want to use base class l-value reference(FrameWorkClassBase&) as a data member in ProductionConsumerClass as i do not have life-time-guarantee/ownership of the referred object.

10
  • 1
    References exhibit late binding behavior without heap allocation Commented Jun 25, 2022 at 5:15
  • I have written entire C++ applications with plenty of late binding and zero heap usage. Commented Jun 25, 2022 at 5:50
  • Read also books on garbage collection (e.g. the GC handbook....) Commented Jun 25, 2022 at 7:22
  • Why do you need m_sptr to be a pointer to FrameWorkClassBase? And a pointer in the first place? Why not FrameWorkClassDerived m_frameworkObject;? Commented Jun 25, 2022 at 8:30
  • because you loose "type substitution", you have essentially tied yourself to an implementation value type rather than a generic base type that would allow you to swap an implementation(derived class) for another implementation. your code is becomes tightly coupled with an implementation. Commented Jun 25, 2022 at 8:48

1 Answer 1

2

There is no need to use heap allocation for late binding to work:

#include <iostream>

struct Base {
  virtual void print() { std::cout << "Base\n"; }
};

struct Derived : public Base {
  void print() override { std::cout << "Derived\n"; }
};

struct Composed {
  Base b;
  Derived d;
};

int main() {
  Base b;
  Derived d;
  Composed c;

  Base &bb{b}, &bd{d}, &cbb{c.b}, &cbd{c.d};

  bb.print();   // Base
  bd.print();   // Derived
  cbb.print();  // Base
  cbd.print();  // Derived
}

In the example above, no heap allocation takes place and all reference variables are of type Base&. Late binding works just fine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.