cok@islsun.Kodak.COM (David Cok) (11/08/90)
The following program will not compile using Sun C++ 2.0 on a SparcStation 1+: class A { public: A(); A(const A&); friend A operator+(A,A); operator void(); }; class B { public: B(); B(const B&); B(A); // constructor to convert A to B implicitly friend B operator+(B,B); operator void(); }; main() { A a; B b; b=a+b; } Without the operator void declarations, this works just fine. As it stands however, the compiler complains CC void_problem.c -c CC void_problem.c: "void_problem.c", line 25: error: ambiguous use of overloaded +: A and B 1 error QUESTION #1: What is going on here? Why does the introduction of an operator void statement introduce ambiguities? The reason I was trying to declare an operator void() is the following. I have some functions which return objects; some of the time these objects are not used in a further expression. Thus the temporaries which contain the objects hang around until the compiler decides to delete them (end of block). However, these objects contain (pointers to) lots of memory. I was hoping that I could ensure their early demise by casting the function result to void and overloading operator void() to call the destructor. I realize that there are dangers here in that my operator void could be used in other situations (e.g. (void)a; ) where destructing would not be a good idea. So, QUESTION #2: Is it reasonable to suggest that the result of an expression statement be implicitly converted to void, and that the default conversion to void is to call the destructor if the object being voided is a temporary? Our problem is that the objects we are dealing with (which are images) are very large, at least they contain pointers to large blocks of memory. We cannot afford to have temporaries survive until the end of the block. As a result, we have had to explicitly manage temporary blocks of image memory using new and delete -- explicitly deleting temporary arguments to a function after they have been used. But this is a real nuisance, not the least of which is worrying about losing memory because some argument in some function somewhere was not deleted. QUESTION #3: Does anyone have any experience or suggestions relating to other ways of dealing with the long lifetime of temporaries? Thanks for any help received (and for just listening!) David R. Cok (replies to cok@Kodak.com) David R. Cok 716-477-7086 B65 - MC01816 e-mail: cok@Kodak.com Eastman Kodak Company Rochester, NY 14650-01816