perry@key.COM (Perry The Cynic) (02/16/91)
Consider the following program: class One { friend class Two; private: One(); }; class Two { static One test; } One Two::test; The last line draws an error from both cfront and Turbo C++: error: ..() cannot access One::One(): private member Why the complaint? My interpretation of the scoping rules says that the Two:: qualifier puts me into Two's scope. Since Two is a friend of One, I don't see why it can't access One's private constructor. Incidentally, the error remains even if I rewrite the last line as One Two::test = One(); and the ARM says explicitly that the initializing expression is interpreted in the context of Two. What am I misunderstanding here? Can somebody quote the language rule that I'm missing? Or are both compilers violating the language standard? Oh, and how do I write this legally? The intent is to make it impossible to create objects of type One, except inside Two (which then hands out references to One objects). Thanks -- perry -- -------------------------------------------------------------------------- Perry The Cynic (Peter Kiehtreiber) perry@arkon.key.com ** What good signature isn't taken yet? ** {amdahl,sgi,pacbell}!key!perry
toan@hpscdc.scd.hp.com (Toan Tran) (02/21/91)
I am not a language lawyer so I am not going to say anything about the behaviour you are getting. But I have a suggestion for what you are trying to do. How about writing a public member function for class Two that calls the class One constructor and use that member function instead. Some thing similar to this: ----------------- #include <iostream.h> class One { friend class Two; private: One(){ cout << "constructing class One instance\n";} }; class Two { public: Two(){ cout << "constructing class Two instance\n";} One *make_one(){return new One();} }; main() { One *ione; Two *itwo; // instantiating itwo of class Two itwo = new Two(); // instantiatin ione of class one by calling Two::make_one() ione = itwo->make_one(); } ------------------- output of the above: constructing class Two instance constructing class One instance I am not sure this is what you want to do but... -- Toan Tran California Design Center/Bipolar Design Section Hewlett Packard toan@cdc.hp.com