i have searched through articles haven't found (yet) quite addresses question. apologies if answer exist somewhere.
a bit of background first...
i want represent device "sections" of functionality, functionality has hierarchical tree-like structure. rather have load of flattened functions
devicereferencecheck(), devicerefereceset(), devicephasesetx(), devicephasedefaultsset...()
i'd instead leverage nested classes get
dev.reference.check() dev.reference.set() dev.phase.setx() dev.phase.defaults.set...()
to i'm trying use nested classes obj.func.subfunction.subsub....()
structure. nested classes need reference outermost class because need use read/write functions provided there.
in attempts, first thing don't understand follows... had tried myself stopped using because of compiler warning.
class gpibdevice_agilent53132a : public gpibdevice { private: class refosc { public: // ... snip ... refosc(gpibdevice_agilent53132a &parent); // ... snip ... } ro; public: // ... snip ... gpibdevice_agilent53132a(); // ... snip ... }; gpibdevice_agilent53132a::gpibdevice_agilent53132a() : gpibdevice(), ro(*this) { }
compiler says: gpibdevice_agilent53132a.cpp(5): warning c4355: 'this' : used in base member initializer list
.
aha, think myself... clever compiler... using this
in initialiser list not idea because class hasn't been constructed yet.
question 1: i've said above correct? using this
, in enclosing class' initialiser list, give nested class reference enclosing class, bad idea? thoughts "yes" clarification because in other threads have seen method being used (nested class member function can't access function of enclosing class. why?).
my approach around have member pointer nested , when in constructor (so safe use class has been constructed) made new inner class pass in reference *this without warnings. standard way of doing it?
continuing on....
the reason private nested class btw don't want user able instantiate class him/herself. now, did have public begin with... tried use private constructor in nested class, compiler told me couldn't construct class. presumably enclosing class can see nested class private data members?
question 2: why can't enclosing class see nested classes private data members/functions?
my work around have nested class declare enclosing class friend. necessary?
thanks guys!
summary
thanks jan explanation of curiously recurring template pattern. it's interesting method know about.
i've ended accepting own answer because feel answers questions directly. crtp method doesn't directly answer questions 1 & 2, provide alternative.
question 1:
it seem possible. thank mkirci , r. martinho fernandes confirming suspicions on why compiler generated warning , whether "safe" ignore it.
in summary... not best idea use this
in initialiser list of constructor because class has not yet been constructed. guys point out, can cause ub if pointer used. i've decided use work around use pointer inner class, , create once inside outer-class constructor... way outer class created , can pass reference inner class safely.
question 2:
from c++ standard, have found (after lot of digging) in section 11.7:
a nested class member , such has same access rights other member. members of enclosing class have no special access members of nested class; usual access rules (clause 11) shall obeyed.
the standard gave following example:
class e { int x; class b { }; class { b b; // ok: e::i can access e::b int y; void f(e* p, int i) { p->x = i; // ok: e::i can access e::x } }; int g(i* p) { return p->y; // error: i::y private } };
so, (annoyingly) why outer class cannot call inner class' private constructor. solution has been make outer class friend of inner class. i.e in above example add friend e;
inner class decl.
Comments
Post a Comment