-1

I need to have a derived class call its new function as seen below. I've tried lots of different variants and positions of keywords.

I create a Base class with two methods PreLoad and Load. The point of PreLoad being to load things that always need to be loaded while Load is used for a class to load what it needs to function.

class Base
    {
    public:
        Base();
        void PreLoad();
        virtual void Load();
    };

    Base::Base() {
        PreLoad();
    }

    void Base::PreLoad() {
        Load();
    }

    class Derived : public Base {
    public:
        virtual void Load() {
            std::cout << "Hia" << std::endl;
        }
    };

    int main()
    {
        Derived d = Derived();
        d.PreLoad();
        return 0;
    }
Jordan Schnur
  • 1,225
  • 3
  • 15
  • 30
  • For polymorphism to work you need to use either references or pointers to the *base* class. Then you initialize the reference or pointer to reference/point to a derived object. E.g. `Base* base = new Derived;` This is actually one of the few cases where pointers are useful in modern C++. But then in "modern" C++ one would use a smart pointer like [`std::unique_ptr`](http://en.cppreference.com/w/cpp/memory/unique_ptr) instead, as in `std::unique_ptr base(new Derived);` – Some programmer dude Sep 25 '16 at 12:24
  • @πάντα ῥεῖ That is why I tried to describe my problem... – Jordan Schnur Sep 25 '16 at 12:26
  • 1
    I don't get what you mean [works as expected](http://coliru.stacked-crooked.com/a/f22d7d3fb686cb31) for me. – πάντα ῥεῖ Sep 25 '16 at 12:27

2 Answers2

3

Your code is lacking an implementation for Load in the base class. You have two options here:

  • Make Load() pure virtual - this option is good when subclasses must override Load(). Demo.
  • Provide a default implementation for Load() - this option is good when classes have an option of overriding Load(), but a default implementation is there. Demo.
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Well this isn't actually my code. It's a mockup of the problem. I currently have a default implementation for Load. However the derived class only ever calls the default implementation. – Jordan Schnur Sep 25 '16 at 12:28
  • @Zyneak This means that you are not calling it in the same way as your `main` shows. I added a second demo that has a default implementation, it works perfectly well. – Sergey Kalinichenko Sep 25 '16 at 12:31
  • That is correct. What actually happens is the class PreLoad from the constructor. I didn't think it had relevance. – Jordan Schnur Sep 25 '16 at 12:32
  • 1
    @Zyneak It has a great deal of relevance! Constructors never call overrides of virtual functions ([see this Q&A for an answer why they don't call overrides](http://stackoverflow.com/q/962132/335858)). – Sergey Kalinichenko Sep 25 '16 at 12:35
  • Thank you for the help. You've helped me solve the problem. I apologize for poor communication and explanations. – Jordan Schnur Sep 25 '16 at 12:41
1

The real problem with your code (assuming you did provide an implementation for Base::Load()) is that a virtual method (Load) is called from within the constructor of Base.

At the time the Base constructor is called, the child (Derived) is not fully instantiated. So the overriden Load will never be called.

GeorgeT
  • 484
  • 3
  • 5