Re: calling a pure virtual from base constructor
* christian.bongiorno@gmail.com:
I am trying to call a pure virtual function from the constructor of an
bastract BaseClass
However, I get a linker error: unresolved symbol. The pure virtual is
defined in the grandparent, the parent invokes the virtual from the
constructor, the child implements it:
class GrandParent {
virtual getClass() = 0;
};
class Parent : public GrandParent {
Parent() { getClass();}
};
class Child : public Parent {
int getClass() { return 10;}
}
How can I do this? I guess I can pass the value in the constructor, but
that's not what I want to do
What you have above seems to be an imagined technical solution to some
problem.
That imagined technical solution is, to use an understatement, Very Very
Bad.
It's Very Very Bad (VVB (TM)) because it puts knowledge in the wrong
place (namely the Parent class), thus creating Very Nasty Dependencies
(VNDs (TM)).
Perhaps the real problem you have is something like a hierarchy of
classes where each class wraps a class in another pre-existing
hierarhcy. Or that you for some other valid reason need to do derived
class specific initialization in the Parent class (the Dynamic Binding
During Initialization (DBDI (TM)) idiom (a.k.a. Calling Virtuals During
Initialization (CVDI (TM))). Then -- voila! -- the FAQ provides a
set of possible techniques.
Read the FAQ item "... is there a way to simulate that behavior as if
dynamic binding worked on the this object within my base class's
constructor?", currently at <url:
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.6>.
If that doesn't help, try to describe here The Real Problem (TRP (TM)).
Shameless push to impress possible employers (current short-term
assignment finished today): that FAQ item, in particular the
how-to-avoid-two-phase-init, originated from a discussion between
Marshall Cline (the FAQ maintainer) and me, after I lamented publicly,
in clc++, that the FAQ was a bit deficient in this area. So it's sort
of "my" FAQ item, although of course it's mainly Marshall's. Ah, but do
employers read clc++m, and are they impressed by such things? Do they
even /understand/ why two-phase init is generally Bad?
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]