Will the following code compile?
class Foo {
protected:
    int a;
};

template<class T> class Setter : public T {
public:
    void Set(int v) { T::a = v; }
};

template<class T> class Incrementer : public T {
public:
    void Inc() { ++T::a; }
};

int main(int argc, char* argv[])
{    
    Setter< Foo > a;
    Incrementer< Foo > b;
    Setter< Incrementer< Foo > > c;

    a.Set(5);   // 1
    a.Inc();    // 2

    b.Set(5);   // 3
    b.Inc();    // 4

    c.Set(5);   // 5
    c.Inc();    // 6

    return 0;
}
Explanation
The variable a is of type Setter, inherited from Foo, this type has no Inc() method;
The variable b is of type Incrementer, inherited from Foo, this type does not have the Set() method;
The variable c is of type Setter, inherited from Incrementer, which is inherited from Foo, this type has both methods Inc() and Set();

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
C++ quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes