いくつかの SO 記事を検索しましたが、私の質問に完全に対応するものは (まだ) 見つかりませんでした。この回答がすでにどこかに存在する場合はお詫び申し上げます。
最初に少し背景を...
機能が階層的なツリーのような構造を持つ、機能の「セクション」を持つデバイスを表現したいと考えています。のようなフラット化された関数をロードするのではなく、
DeviceReferenceCheck(),
DeviceRefereceSet(),
DevicePhaseSetX(),
DevicePhaseDefaultsSet...()
代わりに、ネストされたクラスを活用して、取得できるようにしたいと思います
dev.reference.check()
dev.reference.set()
dev.phase.setx()
dev.phase.defaults.set...()
これを行うために、ネストされたクラスを使用して構造を取得しようとしていますobj.func.subfunction.subsub....()
。ネストされたクラスは、そこで提供される読み取り/書き込み関数を使用する必要があるため、最も外側のクラスへの参照が必要です。
私の試みで、私がよく理解していない最初のことは次のとおりです...私はこれを自分で試しましたが、コンパイラの警告のために使用を中止しました。
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)
{
}
コンパイラは言う: gpibdevice_agilent53132a.cpp(5): warning C4355: 'this' : used in base member initializer list
.
Aha, I think to myself... clever compiler... using this
in the initialiser list is probably not a good idea because the class hasn't been fully constructed yet.
Question 1:
Is what I've said above correct? Is using this
, in the enclosing class' initialiser list, to give the nested class a reference to the enclosing class, a bad idea? My thoughts are "yes" but would like some clarification because in other SO threads I have seen this method being used (Nested Class member function can't access function of enclosing class. Why?).
My approach to get around this was to have a member pointer to nested and then when actually in the constructor (so now safe to use this as class has been constructed) made a new inner class where I could pass in the reference to *this without warnings. Is this the standard way of doing it?
Continuing on....
The reason for the private nested class is btw that I don't want the user to be able to instantiate that class him/herself. Now, I did have it public to begin with... tried to use a private constructor in the nested class, but then the compiler told me it couldn't construct the class. So presumably the enclosing class can see nested class private data members?
Question 2: Why can't the enclosing class see the nested classes private data members/functions?
My work around for this is to have the nested class declare the enclosing class as a friend. Is this really necessary?
Thanks for you help guys!