abstract class with virtual base class and non default constructor

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
 
V

Victor Bazarov

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.

I think making such a distinction between an abstract class and
non-abstract class would be too much for an average compiler. After
all, it might not be so obvious. For instance, the abstractness can be
inherited (and not direct like in your 'A'), or it the inability to
instantiate could be due to some other feature (and not a pure virtual
function). And with conditions like that the standard would be too
complex...

Or it might be just an omission, a defect, that you might want to report
to the committee...

V
 
M

Marcel Müller

I think making such a distinction between an abstract class and
non-abstract class would be too much for an average compiler. After all,
it might not be so obvious. For instance, the abstractness can be
inherited (and not direct like in your 'A'), or it the inability to
instantiate could be due to some other feature (and not a pure virtual
function). And with conditions like that the standard would be too
complex...

Any compiler must know about abstract classes anyway. Simply because it
is an error to create instances of abstract classes.

Or it might be just an omission, a defect, that you might want to report
to the committee...

The more interesting point is what the standard says about this. Maybe
just gcc is broken.


Marcel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,056
Messages
2,570,441
Members
47,119
Latest member
NoeliaIrby

Latest Threads

Top