Abdelrahman Hosny
Abdelrahman Hosny

Reputation: 15

Can I have a static function get inherited with a specific value to each class?

Is there a way to make it so that each of Special and normal employees each have their own unique counter and that the function in the base class uses the class specific counter if the function is called from NormalEmp or SpecialEmp object

class Employee {

public:
    static int count ;
    Employee(){ count++;}
    void foo(){
        if(count > 50)
        //do 1,2,3
    }

};

int Employee::count = 0;

class NormalEmp :public  Employee
{
};

class SpecialEmp :public  Employee
{
};

Upvotes: 0

Views: 27

Answers (1)

Lukas-T
Lukas-T

Reputation: 11340

If I understand you correctly you could retrieve count via a (pure) virtual getter:

class Employee {
public:
    virtual int getCount() const = 0;

    void foo(){
        auto count = this->getCount();
        // ...     
    }

};

class NormalEmp :public  Employee
{
public:
    static int count;

    int getCount() const override {
        return NormalEmp::count;
    }
};

SpecialEmp would look the same, it's just the basic idea.

Upvotes: 1

Related Questions