C++ question

Neil Stevens neil at qualityassistant.com
Wed Sep 25 12:33:15 BST 2002


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wednesday September 25, 2002 04:25, Michael Goffioul wrote:
> Can I call a pure virtual function in the constructor of the abstract
> class? That is
>
> class A
> {
> public:
>    A() { func(); }
>    virtual void func() = 0;
> };

No, and calling a virtual in the constructor of a non-abstract class won't 
do what you want it to, either.  In a base class constructor, the child 
classes haven't been initialized and haven't put their virtuals in to the 
table yet.

So when you want a constructor to call virtuals, you need to have a 
separate initialization function to be called after the constructor.  And 
when you do that, you probably have to have some sort of Factory for that 
class.

- -- 
Neil Stevens - neil at qualityassistant.com
"I always cheer up immensely if an attack is particularly wounding
because I think, well, if they attack one personally, it means they
have not a single political argument left." - Margaret Thatcher
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE9kZ77f7mnligQOmERAq+ZAJ4yRH3/ObVVIFtH5GO2/AXGbRhpTgCfepe3
nYxklCDo666awmcVR+NiisM=
=IPZG
-----END PGP SIGNATURE-----





More information about the kde-core-devel mailing list