M
Marcel Müller
Virtual base classes are always constructed by the most derived class.
So why need an abstract class a call to the constructor of it's virtual
base?
#include <stdio.h>
class V
{protected:
V(int i) { printf("V::V(%i)\n", i); }
};
class A : public virtual V
{protected:
A() : V(42) {} // <-- required to keep the compiler happy
virtual void PureFunc() = 0;
};
class D : public A
{public:
D(int i) : V(i) {}
protected:
void PureFunc() {}
};
int main()
{ D d(7);
}
From my understanding the constructor call V(42) could never be
executed, isn't it? A cannot be instantiated and any class inheriting
from A must initialize V::V(int) explicitly.
Marcel
So why need an abstract class a call to the constructor of it's virtual
base?
#include <stdio.h>
class V
{protected:
V(int i) { printf("V::V(%i)\n", i); }
};
class A : public virtual V
{protected:
A() : V(42) {} // <-- required to keep the compiler happy
virtual void PureFunc() = 0;
};
class D : public A
{public:
D(int i) : V(i) {}
protected:
void PureFunc() {}
};
int main()
{ D d(7);
}
From my understanding the constructor call V(42) could never be
executed, isn't it? A cannot be instantiated and any class inheriting
from A must initialize V::V(int) explicitly.
Marcel