Search code examples
c++inheritancevirtual

Avoiding "Pure Virtual Function Call" in Derived Class C++


I'm reasonably new to C++ so I'd like to apologize if the level of this question is a little below the usual standards here -

I'm trying to get several classes to inherit from a base class which has a virtual function definition, then I'd like to create an array of MainClass* which can include all of the derived classes in order to output the derived + defined virtual function.

I receive the Error "R6025: pure virtual function call" - I don't know why, I was assuming that - when called - the updated definition of that function from the child classes was used.

Here's my code:

Base:

class mitarbeiter
{
public:
    mitarbeiter(string name, int nummer);
    ~mitarbeiter(void);
    virtual void einkommen() = 0;
protected:
    string name;
    int nummer;

};

Derived1:

#include "mitarbeiter.h"
class lohnempfaenger : public mitarbeiter
{
public:
    lohnempfaenger(int stundenlohn, int anzahlStunden, string name, int nummer);
    ~lohnempfaenger(void);
    void einkommen();
private:
    int stundenlohn;
    int anzahlStunden;
};

Derived 2:

#include "mitarbeiter.h"
#include <string>
class angestellter : public mitarbeiter
{
public:
    angestellter(int gehalt, string name, int nummer);
    ~angestellter(void);
    void einkommen();
private:
    int gehalt;
};

Implementation of "einkommen()" in Derived 1:

void lohnempfaenger::einkommen()
{
    cout << "Lohnempfaenger, Name: " << this->name << ", Gesamtlohn: " << this->stundenlohn*this->anzahlStunden << "\n";
}

Implementation of "einkommen()" in Derived 2:

void angestellter::einkommen()
{
    cout << "Angestellter, Name: " << this->name << ", Einkommen: " << this->gehalt << "\n";
}

Array-Declaration in main method:

mitarbeiter* mitPtr[5];
mitPtr[0] = &angestellter(/*values...*/);
//error
mitPtr[0]->einkommen();

Constructors:

mitarbeiter::mitarbeiter(string name, int nummer)
{
    this->name = name;
    this->nummer = nummer;
}

angestellter::angestellter(int gehalt, string name, int nummer):mitarbeiter(name, nummer)
{
    this->gehalt = gehalt;
}

lohnempfaenger::lohnempfaenger(int stundenlohn, int anzahlStunden, string name, int nummer):mitarbeiter(name, nummer)
{
    this->stundenlohn = stundenlohn;
    this->anzahlStunden = anzahlStunden;
}

Thanks a lot!


Solution

  • Now you've shown us how you actually initialise the pointer, here is the problem:

    mitPtr[0] = &angestellter(/*values...*/);
    

    That points to a temporary object, which is destroyed at the end of the statement. Using the pointer afterwards will give undefined behaviour.

    You'll need a non-temporary variable to point to:

    angestellter a(/*values...*/);
    mitPtr[0] = &a;
    

    or perhaps a dynamically allocated object:

    mitPtr[0] = new angestellter(...);
    

    In that case, don't forget to delete it; and don't forget to give the base class a virtual destructor so it can be deleted. You might consider using a smart pointer to delete it for you; std::unique_ptr would be ideal.