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();

Слідкуй за CodeGalaxy

Мобільний додаток Beta

Get it on Google Play
Зворотній Зв’язок
Cosmo
Зареєструйся Зараз
або Підпишись на майбутні тести