   [c++] Initialization and Inheritance.
  I have run across a difficult bug to track down. I suspect I know where it is, but  am not sure. The code is also fairly convoluted, so I'm going to post only the parts I suspect have the problemThe scenario involves 3 classes. The first is a common templated functor. The second is a larger templated structure which contains as a member, the first. The third is a non-templated structure which inherits from the second, as well as being a parameter to the inheritance...Quite convoluted. I've a sneaking suspicion that the initializer for the first class in the 2nd's constructor isn't being called nicely when the 3rd's is run. I'm not sure if this isn't expected behavior, as I've not run across this sort of pattern before.Anyways, code in question:template <typename T,typename M,typename S>struct  lookup{//// Templated Associative Container Lookup functor.//// <Stored Type, Container Type, Key Type>//   M  &m;   T  *operator()(S in){ typename M::iterator    imit; imit=m.find(in); if (imit==m.end()){    return(0); } return(imit->second);   }   lookup(M &inm):m(inm){}};template <typename T,typename M1,typename M2,typename E1=equal_to<M1>,typename E2=equal_to<M2> >struct  bdmap{// *snip* *snip*   typedef hash_map<M1,T *,hash<M1>,E1>    amap_shortname;   typedef hash_map<M2,T *,hash<M2>,E2>    bmap_shortname;   amap_shortname amap;   bmap_shortname bmap;   lookup<T,amap_shortname,M1>   m1tot;   lookup<T,bmap_shortname,M2>   m2tot;   bdmap(M1 T::* inm1, M2 T::* inm2, M1 fail_const):m1(inm1),m2(inm2),m1tot(amap),m2tot(bmap),nextid(0){}// *snip* *snip*};struct  bdtree:public bdmap<bdtree,string,int>{//// *snip* *snip*//bdtree(string   inname=string(""),int maxdepth=-1):name(inname),id(0),parent(0),depth(maxdepth),bdmap<bdtree,string,int>(&bdtree::name,&bdtree::id,string("failure")){}bdtree  *add_child(string inname){ intrtnid; bool    pf; bdtree  *bdt; if (!depth){    return(0); } if (depth!=-1){    pf=add_entry(inname,bdtree(inname,depth-1),&rtnid); }else{    pf=add_entry(inname,bdtree(inname,-1),&rtnid); } if (pf){    // add_entry above, returns the created id in    //  rtnid.    //    // Somehow, id_to_object [which runs the id lookup]    //  can't find it though!    //    bdt=id_to_object(rtnid);    if (bdt){  bdt->parent=this;    }    return(bdt); }else{    return(0); }   }// *snip* *snip*};Now, this bug manifests itself such that the functors [m1tot,m2tot] return 0 for the ID just added to the hash map, but only in the 2nd level of the tree or lower... They work just fine for a "flat" instance. Further, manually iterating through the hash_map's indicate that add_entry works, and the actual id was added.The only thing I can think is that somehow m1tot and m2tot aren't being initialized with the correct hash_map to do the lookup on. Since that code is inlined by the compiler, stepping through doesn't help. Since the functor is templated to high heaven, I can't easily find it to set a break. And I doubt it actually would even if I could.Perhaps there's just some constructor rules I don't know? I tried explicitly initializing them like is necissary for multiple inheritance classes, but that produced many errors...  Is the bug perhaps elsewhere?Let me know if the full code would be more helpful, and if you'd actually take the time to look through such garbage.   
  Base classes *must* be initialized before members. I'm surprised the compiler doesn't complain.Check out boost::base_from_member;  Ah. A slight redesign then I suppose. Odd that it would work some times and not others.Curse ye, undefined behavior! *shakes fist*[edit: oh, and rate up for the helpful hardy]   