Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!ucselx!petunia!kestrel.edu!gyro From: gyro@kestrel.edu (Scott Layson) Newsgroups: comp.lang.c++ Subject: Re: possible to overload << and pass an object ptr? Summary: Two-level class structure Keywords: overload Message-ID: <1991Mar24.074308.28666@kestrel.edu> Date: 24 Mar 91 07:43:08 GMT References: Organization: Kestrel Institute, Palo Alto, CA Lines: 42 In article todd@Quotron.COM (Todd Booth) writes: >Consider the following struct: > >struct S { > void operator<<(char p1) { cout << p1; } >}; > >S *o2 = new S; >o2 << 'a'; // error <--- How can I resolve this? >*o2 << 'a'; // no problem, but ugly > >S o1; >o1 << 'a'; // no problem I have used a two-level class structure in cases like this: struct S { struct S_impl { // ... whatever } *impl; public: void operator<<(char p1) { cout << p1; } // constructors etc. }; This is a pretty good solution, but it starts to break down when you want to have parallel hierarchies of inner and outer classes, because `impl' (the sole data member of S which will be inherited by all of its derived classes) is always of type `S_impl*', while you will want it to be a pointer to the appropriate derived class of `S_impl'. Casting `impl' to be the right type is a bit messy but works, until you start using multiple inheritance with virtual base classes, at which point you're totally up a tree. For these reasons I have toyed with the idea of proposing to the committee a system for declaring "member functions" on the type `C*' where C is some class, so that your first example could be made to work without the two-level class structure. Unfortunately, I have never managed to come up with a proposal I find adequately elegant. -- Scott Layson Burson Gyro@Reasoning.COM