Monday, March 17, 2014

abstract class with virtual base class and non default constructor

Newsgroup: comp.lang.c++

Subject: abstract class with virtual base class and non default constructor

From: =?ISO-8859-1?Q?Marcel_M=FCller?= <news.5.maazl@...>

Date: Sat, 15 Mar 2014 18:26:54 +0100



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







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://ift.tt/1nzip4T

View all the progranning help forums at:

http://ift.tt/1dP9txN

No comments:

Post a Comment